1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-15 14:03:06 +02:00

Store commit.Action as an enum instead of a string

The main reason for doing this (besides the reasons given for Status in the
previous commit) is that it allows us to easily convert from TodoCommand to
Action and back. This will be needed later in the branch. Fortunately,
TodoCommand is one-based, so this allows us to add an ActionNone constant with
the value 0.
This commit is contained in:
Stefan Haller 2023-04-03 12:42:29 +02:00
parent 188773511e
commit c53c5e47ef
7 changed files with 57 additions and 45 deletions

View File

@ -313,7 +313,7 @@ func (self *CommitLoader) getInteractiveRebasingCommits() ([]*models.Commit, err
Sha: t.Commit, Sha: t.Commit,
Name: t.Msg, Name: t.Msg,
Status: models.StatusRebasing, Status: models.StatusRebasing,
Action: t.Command.String(), Action: t.Command,
}) })
} }

View File

@ -79,7 +79,7 @@ func TestGetCommits(t *testing.T) {
Sha: "0eea75e8c631fba6b58135697835d58ba4c18dbc", Sha: "0eea75e8c631fba6b58135697835d58ba4c18dbc",
Name: "better typing for rebase mode", Name: "better typing for rebase mode",
Status: models.StatusUnpushed, Status: models.StatusUnpushed,
Action: "", Action: models.ActionNone,
Tags: []string{}, Tags: []string{},
ExtraInfo: "(HEAD -> better-tests)", ExtraInfo: "(HEAD -> better-tests)",
AuthorName: "Jesse Duffield", AuthorName: "Jesse Duffield",
@ -93,7 +93,7 @@ func TestGetCommits(t *testing.T) {
Sha: "b21997d6b4cbdf84b149d8e6a2c4d06a8e9ec164", Sha: "b21997d6b4cbdf84b149d8e6a2c4d06a8e9ec164",
Name: "fix logging", Name: "fix logging",
Status: models.StatusPushed, Status: models.StatusPushed,
Action: "", Action: models.ActionNone,
Tags: []string{}, Tags: []string{},
ExtraInfo: "(origin/better-tests)", ExtraInfo: "(origin/better-tests)",
AuthorName: "Jesse Duffield", AuthorName: "Jesse Duffield",
@ -107,7 +107,7 @@ func TestGetCommits(t *testing.T) {
Sha: "e94e8fc5b6fab4cb755f29f1bdb3ee5e001df35c", Sha: "e94e8fc5b6fab4cb755f29f1bdb3ee5e001df35c",
Name: "refactor", Name: "refactor",
Status: models.StatusPushed, Status: models.StatusPushed,
Action: "", Action: models.ActionNone,
Tags: []string{}, Tags: []string{},
ExtraInfo: "", ExtraInfo: "",
AuthorName: "Jesse Duffield", AuthorName: "Jesse Duffield",
@ -121,7 +121,7 @@ func TestGetCommits(t *testing.T) {
Sha: "d8084cd558925eb7c9c38afeed5725c21653ab90", Sha: "d8084cd558925eb7c9c38afeed5725c21653ab90",
Name: "WIP", Name: "WIP",
Status: models.StatusPushed, Status: models.StatusPushed,
Action: "", Action: models.ActionNone,
Tags: []string{}, Tags: []string{},
ExtraInfo: "", ExtraInfo: "",
AuthorName: "Jesse Duffield", AuthorName: "Jesse Duffield",
@ -135,7 +135,7 @@ func TestGetCommits(t *testing.T) {
Sha: "65f910ebd85283b5cce9bf67d03d3f1a9ea3813a", Sha: "65f910ebd85283b5cce9bf67d03d3f1a9ea3813a",
Name: "WIP", Name: "WIP",
Status: models.StatusPushed, Status: models.StatusPushed,
Action: "", Action: models.ActionNone,
Tags: []string{}, Tags: []string{},
ExtraInfo: "", ExtraInfo: "",
AuthorName: "Jesse Duffield", AuthorName: "Jesse Duffield",
@ -149,7 +149,7 @@ func TestGetCommits(t *testing.T) {
Sha: "26c07b1ab33860a1a7591a0638f9925ccf497ffa", Sha: "26c07b1ab33860a1a7591a0638f9925ccf497ffa",
Name: "WIP", Name: "WIP",
Status: models.StatusMerged, Status: models.StatusMerged,
Action: "", Action: models.ActionNone,
Tags: []string{}, Tags: []string{},
ExtraInfo: "", ExtraInfo: "",
AuthorName: "Jesse Duffield", AuthorName: "Jesse Duffield",
@ -163,7 +163,7 @@ func TestGetCommits(t *testing.T) {
Sha: "3d4470a6c072208722e5ae9a54bcb9634959a1c5", Sha: "3d4470a6c072208722e5ae9a54bcb9634959a1c5",
Name: "WIP", Name: "WIP",
Status: models.StatusMerged, Status: models.StatusMerged,
Action: "", Action: models.ActionNone,
Tags: []string{}, Tags: []string{},
ExtraInfo: "", ExtraInfo: "",
AuthorName: "Jesse Duffield", AuthorName: "Jesse Duffield",
@ -177,7 +177,7 @@ func TestGetCommits(t *testing.T) {
Sha: "053a66a7be3da43aacdc7aa78e1fe757b82c4dd2", Sha: "053a66a7be3da43aacdc7aa78e1fe757b82c4dd2",
Name: "refactoring the config struct", Name: "refactoring the config struct",
Status: models.StatusMerged, Status: models.StatusMerged,
Action: "", Action: models.ActionNone,
Tags: []string{}, Tags: []string{},
ExtraInfo: "", ExtraInfo: "",
AuthorName: "Jesse Duffield", AuthorName: "Jesse Duffield",

View File

@ -6,6 +6,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/fsmiamoto/git-todo-parser/todo"
"github.com/go-errors/errors" "github.com/go-errors/errors"
"github.com/jesseduffield/generics/slices" "github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/app/daemon" "github.com/jesseduffield/lazygit/pkg/app/daemon"
@ -202,7 +203,7 @@ func (self *RebaseCommands) AmendTo(commit *models.Commit) error {
} }
// EditRebaseTodo sets the action at a given index in the git-rebase-todo file // EditRebaseTodo sets the action at a given index in the git-rebase-todo file
func (self *RebaseCommands) EditRebaseTodo(index int, action string) error { func (self *RebaseCommands) EditRebaseTodo(index int, action todo.TodoCommand) error {
fileName := filepath.Join(self.dotGitDir, "rebase-merge/git-rebase-todo") fileName := filepath.Join(self.dotGitDir, "rebase-merge/git-rebase-todo")
bytes, err := os.ReadFile(fileName) bytes, err := os.ReadFile(fileName)
if err != nil { if err != nil {
@ -216,7 +217,7 @@ func (self *RebaseCommands) EditRebaseTodo(index int, action string) error {
// it at the bottom, so we need to subtract our index from the commit count // it at the bottom, so we need to subtract our index from the commit count
contentIndex := commitCount - 1 - index contentIndex := commitCount - 1 - index
splitLine := strings.Split(content[contentIndex], " ") splitLine := strings.Split(content[contentIndex], " ")
content[contentIndex] = action + " " + strings.Join(splitLine[1:], " ") content[contentIndex] = action.String() + " " + strings.Join(splitLine[1:], " ")
result := strings.Join(content, "\n") result := strings.Join(content, "\n")
return os.WriteFile(fileName, []byte(result), 0o644) return os.WriteFile(fileName, []byte(result), 0o644)

View File

@ -3,6 +3,7 @@ package models
import ( import (
"fmt" "fmt"
"github.com/fsmiamoto/git-todo-parser/todo"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
) )
@ -21,12 +22,18 @@ const (
StatusReflog StatusReflog
) )
const (
// Conveniently for us, the todo package starts the enum at 1, and given
// that it doesn't have a "none" value, we're setting ours to 0
ActionNone todo.TodoCommand = 0
)
// Commit : A git commit // Commit : A git commit
type Commit struct { type Commit struct {
Sha string Sha string
Name string Name string
Status CommitStatus Status CommitStatus
Action string // one of "", "pick", "edit", "squash", "reword", "drop", "fixup" Action todo.TodoCommand
Tags []string Tags []string
ExtraInfo string // something like 'HEAD -> master, tag: v0.15.2' ExtraInfo string // something like 'HEAD -> master, tag: v0.15.2'
AuthorName string // something like 'Jesse Duffield' AuthorName string // something like 'Jesse Duffield'
@ -75,7 +82,7 @@ func (c *Commit) IsMerge() bool {
// returns true if this commit is not actually in the git log but instead // returns true if this commit is not actually in the git log but instead
// is from a TODO file for an interactive rebase. // is from a TODO file for an interactive rebase.
func (c *Commit) IsTODO() bool { func (c *Commit) IsTODO() bool {
return c.Action != "" return c.Action != ActionNone
} }
func IsHeadCommit(commits []*Commit, index int) bool { func IsHeadCommit(commits []*Commit, index int) bool {

View File

@ -3,6 +3,7 @@ package controllers
import ( import (
"fmt" "fmt"
"github.com/fsmiamoto/git-todo-parser/todo"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/types/enums" "github.com/jesseduffield/lazygit/pkg/commands/types/enums"
"github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/context"
@ -153,7 +154,7 @@ func (self *LocalCommitsController) squashDown(commit *models.Commit) error {
return self.c.ErrorMsg(self.c.Tr.CannotSquashOrFixupFirstCommit) return self.c.ErrorMsg(self.c.Tr.CannotSquashOrFixupFirstCommit)
} }
applied, err := self.handleMidRebaseCommand("squash", commit) applied, err := self.handleMidRebaseCommand(todo.Squash, commit)
if err != nil { if err != nil {
return err return err
} }
@ -178,7 +179,7 @@ func (self *LocalCommitsController) fixup(commit *models.Commit) error {
return self.c.ErrorMsg(self.c.Tr.CannotSquashOrFixupFirstCommit) return self.c.ErrorMsg(self.c.Tr.CannotSquashOrFixupFirstCommit)
} }
applied, err := self.handleMidRebaseCommand("fixup", commit) applied, err := self.handleMidRebaseCommand(todo.Fixup, commit)
if err != nil { if err != nil {
return err return err
} }
@ -199,7 +200,7 @@ func (self *LocalCommitsController) fixup(commit *models.Commit) error {
} }
func (self *LocalCommitsController) reword(commit *models.Commit) error { func (self *LocalCommitsController) reword(commit *models.Commit) error {
applied, err := self.handleMidRebaseCommand("reword", commit) applied, err := self.handleMidRebaseCommand(todo.Reword, commit)
if err != nil { if err != nil {
return err return err
} }
@ -248,7 +249,7 @@ func (self *LocalCommitsController) doRewordEditor() error {
} }
func (self *LocalCommitsController) rewordEditor(commit *models.Commit) error { func (self *LocalCommitsController) rewordEditor(commit *models.Commit) error {
midRebase, err := self.handleMidRebaseCommand("reword", commit) midRebase, err := self.handleMidRebaseCommand(todo.Reword, commit)
if err != nil { if err != nil {
return err return err
} }
@ -268,7 +269,7 @@ func (self *LocalCommitsController) rewordEditor(commit *models.Commit) error {
} }
func (self *LocalCommitsController) drop(commit *models.Commit) error { func (self *LocalCommitsController) drop(commit *models.Commit) error {
applied, err := self.handleMidRebaseCommand("drop", commit) applied, err := self.handleMidRebaseCommand(todo.Drop, commit)
if err != nil { if err != nil {
return err return err
} }
@ -289,7 +290,7 @@ func (self *LocalCommitsController) drop(commit *models.Commit) error {
} }
func (self *LocalCommitsController) edit(commit *models.Commit) error { func (self *LocalCommitsController) edit(commit *models.Commit) error {
applied, err := self.handleMidRebaseCommand("edit", commit) applied, err := self.handleMidRebaseCommand(todo.Edit, commit)
if err != nil { if err != nil {
return err return err
} }
@ -305,7 +306,7 @@ func (self *LocalCommitsController) edit(commit *models.Commit) error {
} }
func (self *LocalCommitsController) pick(commit *models.Commit) error { func (self *LocalCommitsController) pick(commit *models.Commit) error {
applied, err := self.handleMidRebaseCommand("pick", commit) applied, err := self.handleMidRebaseCommand(todo.Pick, commit)
if err != nil { if err != nil {
return err return err
} }
@ -326,12 +327,12 @@ func (self *LocalCommitsController) interactiveRebase(action string) error {
// handleMidRebaseCommand sees if the selected commit is in fact a rebasing // handleMidRebaseCommand sees if the selected commit is in fact a rebasing
// commit meaning you are trying to edit the todo file rather than actually // commit meaning you are trying to edit the todo file rather than actually
// begin a rebase. It then updates the todo file with that action // begin a rebase. It then updates the todo file with that action
func (self *LocalCommitsController) handleMidRebaseCommand(action string, commit *models.Commit) (bool, error) { func (self *LocalCommitsController) handleMidRebaseCommand(action todo.TodoCommand, commit *models.Commit) (bool, error) {
if !commit.IsTODO() { if !commit.IsTODO() {
if self.git.Status.WorkingTreeState() != enums.REBASE_MODE_NONE { if self.git.Status.WorkingTreeState() != enums.REBASE_MODE_NONE {
// If we are in a rebase, the only action that is allowed for // If we are in a rebase, the only action that is allowed for
// non-todo commits is rewording the current head commit // non-todo commits is rewording the current head commit
if !(action == "reword" && self.isHeadCommit()) { if !(action == todo.Reword && self.isHeadCommit()) {
return true, self.c.ErrorMsg(self.c.Tr.AlreadyRebasing) return true, self.c.ErrorMsg(self.c.Tr.AlreadyRebasing)
} }
} }
@ -343,13 +344,13 @@ func (self *LocalCommitsController) handleMidRebaseCommand(action string, commit
// and that means we either unconditionally wait around for the subprocess to ask for // and that means we either unconditionally wait around for the subprocess to ask for
// our input or we set a lazygit client as the EDITOR env variable and have it // our input or we set a lazygit client as the EDITOR env variable and have it
// request us to edit the commit message when prompted. // request us to edit the commit message when prompted.
if action == "reword" { if action == todo.Reword {
return true, self.c.ErrorMsg(self.c.Tr.LcRewordNotSupported) return true, self.c.ErrorMsg(self.c.Tr.LcRewordNotSupported)
} }
self.c.LogAction("Update rebase TODO") self.c.LogAction("Update rebase TODO")
self.c.LogCommand( self.c.LogCommand(
fmt.Sprintf("Updating rebase action of commit %s to '%s'", commit.ShortSha(), action), fmt.Sprintf("Updating rebase action of commit %s to '%s'", commit.ShortSha(), action.String()),
false, false,
) )

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/fsmiamoto/git-todo-parser/todo"
"github.com/jesseduffield/generics/set" "github.com/jesseduffield/generics/set"
"github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/git_commands"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
@ -263,8 +264,9 @@ func displayCommit(
bisectString := getBisectStatusText(bisectStatus, bisectInfo) bisectString := getBisectStatusText(bisectStatus, bisectInfo)
actionString := "" actionString := ""
if commit.Action != "" { if commit.Action != models.ActionNone {
actionString = actionColorMap(commit.Action).Sprint(commit.Action) + " " todoString := commit.Action.String()
actionString = actionColorMap(commit.Action).Sprint(todoString) + " "
} }
tagString := "" tagString := ""
@ -368,15 +370,15 @@ func getShaColor(
return shaColor return shaColor
} }
func actionColorMap(str string) style.TextStyle { func actionColorMap(action todo.TodoCommand) style.TextStyle {
switch str { switch action {
case "pick": case todo.Pick:
return style.FgCyan return style.FgCyan
case "drop": case todo.Drop:
return style.FgRed return style.FgRed
case "edit": case todo.Edit:
return style.FgGreen return style.FgGreen
case "fixup": case todo.Fixup:
return style.FgMagenta return style.FgMagenta
default: default:
return style.FgYellow return style.FgYellow

View File

@ -5,6 +5,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/fsmiamoto/git-todo-parser/todo"
"github.com/gookit/color" "github.com/gookit/color"
"github.com/jesseduffield/generics/set" "github.com/jesseduffield/generics/set"
"github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/git_commands"
@ -91,8 +92,8 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
{ {
testName: "showing graph, including rebase commits", testName: "showing graph, including rebase commits",
commits: []*models.Commit{ commits: []*models.Commit{
{Name: "commit1", Sha: "sha1", Parents: []string{"sha2", "sha3"}, Action: "pick"}, {Name: "commit1", Sha: "sha1", Parents: []string{"sha2", "sha3"}, Action: todo.Pick},
{Name: "commit2", Sha: "sha2", Parents: []string{"sha3"}, Action: "pick"}, {Name: "commit2", Sha: "sha2", Parents: []string{"sha3"}, Action: todo.Pick},
{Name: "commit3", Sha: "sha3", Parents: []string{"sha4"}}, {Name: "commit3", Sha: "sha3", Parents: []string{"sha4"}},
{Name: "commit4", Sha: "sha4", Parents: []string{"sha5"}}, {Name: "commit4", Sha: "sha4", Parents: []string{"sha5"}},
{Name: "commit5", Sha: "sha5", Parents: []string{"sha7"}}, {Name: "commit5", Sha: "sha5", Parents: []string{"sha7"}},
@ -114,8 +115,8 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
{ {
testName: "showing graph, including rebase commits, with offset", testName: "showing graph, including rebase commits, with offset",
commits: []*models.Commit{ commits: []*models.Commit{
{Name: "commit1", Sha: "sha1", Parents: []string{"sha2", "sha3"}, Action: "pick"}, {Name: "commit1", Sha: "sha1", Parents: []string{"sha2", "sha3"}, Action: todo.Pick},
{Name: "commit2", Sha: "sha2", Parents: []string{"sha3"}, Action: "pick"}, {Name: "commit2", Sha: "sha2", Parents: []string{"sha3"}, Action: todo.Pick},
{Name: "commit3", Sha: "sha3", Parents: []string{"sha4"}}, {Name: "commit3", Sha: "sha3", Parents: []string{"sha4"}},
{Name: "commit4", Sha: "sha4", Parents: []string{"sha5"}}, {Name: "commit4", Sha: "sha4", Parents: []string{"sha5"}},
{Name: "commit5", Sha: "sha5", Parents: []string{"sha7"}}, {Name: "commit5", Sha: "sha5", Parents: []string{"sha7"}},
@ -136,8 +137,8 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
{ {
testName: "startIdx is past TODO commits", testName: "startIdx is past TODO commits",
commits: []*models.Commit{ commits: []*models.Commit{
{Name: "commit1", Sha: "sha1", Parents: []string{"sha2", "sha3"}, Action: "pick"}, {Name: "commit1", Sha: "sha1", Parents: []string{"sha2", "sha3"}, Action: todo.Pick},
{Name: "commit2", Sha: "sha2", Parents: []string{"sha3"}, Action: "pick"}, {Name: "commit2", Sha: "sha2", Parents: []string{"sha3"}, Action: todo.Pick},
{Name: "commit3", Sha: "sha3", Parents: []string{"sha4"}}, {Name: "commit3", Sha: "sha3", Parents: []string{"sha4"}},
{Name: "commit4", Sha: "sha4", Parents: []string{"sha5"}}, {Name: "commit4", Sha: "sha4", Parents: []string{"sha5"}},
{Name: "commit5", Sha: "sha5", Parents: []string{"sha7"}}, {Name: "commit5", Sha: "sha5", Parents: []string{"sha7"}},
@ -156,8 +157,8 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
{ {
testName: "only showing TODO commits", testName: "only showing TODO commits",
commits: []*models.Commit{ commits: []*models.Commit{
{Name: "commit1", Sha: "sha1", Parents: []string{"sha2", "sha3"}, Action: "pick"}, {Name: "commit1", Sha: "sha1", Parents: []string{"sha2", "sha3"}, Action: todo.Pick},
{Name: "commit2", Sha: "sha2", Parents: []string{"sha3"}, Action: "pick"}, {Name: "commit2", Sha: "sha2", Parents: []string{"sha3"}, Action: todo.Pick},
{Name: "commit3", Sha: "sha3", Parents: []string{"sha4"}}, {Name: "commit3", Sha: "sha3", Parents: []string{"sha4"}},
{Name: "commit4", Sha: "sha4", Parents: []string{"sha5"}}, {Name: "commit4", Sha: "sha4", Parents: []string{"sha5"}},
{Name: "commit5", Sha: "sha5", Parents: []string{"sha7"}}, {Name: "commit5", Sha: "sha5", Parents: []string{"sha7"}},
@ -195,10 +196,10 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
{ {
testName: "only TODO commits except last", testName: "only TODO commits except last",
commits: []*models.Commit{ commits: []*models.Commit{
{Name: "commit1", Sha: "sha1", Parents: []string{"sha2", "sha3"}, Action: "pick"}, {Name: "commit1", Sha: "sha1", Parents: []string{"sha2", "sha3"}, Action: todo.Pick},
{Name: "commit2", Sha: "sha2", Parents: []string{"sha3"}, Action: "pick"}, {Name: "commit2", Sha: "sha2", Parents: []string{"sha3"}, Action: todo.Pick},
{Name: "commit3", Sha: "sha3", Parents: []string{"sha4"}, Action: "pick"}, {Name: "commit3", Sha: "sha3", Parents: []string{"sha4"}, Action: todo.Pick},
{Name: "commit4", Sha: "sha4", Parents: []string{"sha5"}, Action: "pick"}, {Name: "commit4", Sha: "sha4", Parents: []string{"sha5"}, Action: todo.Pick},
{Name: "commit5", Sha: "sha5", Parents: []string{"sha7"}}, {Name: "commit5", Sha: "sha5", Parents: []string{"sha7"}},
}, },
startIdx: 0, startIdx: 0,
@ -215,7 +216,7 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
{ {
testName: "don't show YOU ARE HERE label when not asked for (e.g. in branches panel)", testName: "don't show YOU ARE HERE label when not asked for (e.g. in branches panel)",
commits: []*models.Commit{ commits: []*models.Commit{
{Name: "commit1", Sha: "sha1", Parents: []string{"sha2"}, Action: "pick"}, {Name: "commit1", Sha: "sha1", Parents: []string{"sha2"}, Action: todo.Pick},
{Name: "commit2", Sha: "sha2", Parents: []string{"sha3"}}, {Name: "commit2", Sha: "sha2", Parents: []string{"sha3"}},
{Name: "commit3", Sha: "sha3", Parents: []string{"sha4"}}, {Name: "commit3", Sha: "sha3", Parents: []string{"sha4"}},
}, },