1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-17 01:42:45 +02:00

Store commit.Status as an enum instead of a string

This is unrelated to the changes in this PR, but since we are doing the same
thing for the commit.Action field in the next commit, it makes sense to do it
for Status too for consistency. Modelling this as an enum feels more natural
than modelling it as a string, since there's a finite set of possible values.
And it saves a little bit of memory (not very much, since none of the strings
were heap-allocated, but still).
This commit is contained in:
Stefan Haller
2023-04-03 12:40:29 +02:00
parent 62c5c32fbb
commit 188773511e
6 changed files with 42 additions and 29 deletions

View File

@ -89,7 +89,7 @@ func (self *CommitLoader) GetCommits(opts GetCommitsOptions) ([]*models.Commit,
if commit.Sha == firstPushedCommit { if commit.Sha == firstPushedCommit {
passedFirstPushedCommit = true passedFirstPushedCommit = true
} }
commit.Status = map[bool]string{true: "unpushed", false: "pushed"}[!passedFirstPushedCommit] commit.Status = map[bool]models.CommitStatus{true: models.StatusUnpushed, false: models.StatusPushed}[!passedFirstPushedCommit]
commits = append(commits, commit) commits = append(commits, commit)
return false, nil return false, nil
}) })
@ -312,7 +312,7 @@ func (self *CommitLoader) getInteractiveRebasingCommits() ([]*models.Commit, err
commits = slices.Prepend(commits, &models.Commit{ commits = slices.Prepend(commits, &models.Commit{
Sha: t.Commit, Sha: t.Commit,
Name: t.Msg, Name: t.Msg,
Status: "rebasing", Status: models.StatusRebasing,
Action: t.Command.String(), Action: t.Command.String(),
}) })
} }
@ -332,7 +332,7 @@ func (self *CommitLoader) commitFromPatch(content string) *models.Commit {
return &models.Commit{ return &models.Commit{
Sha: sha, Sha: sha,
Name: name, Name: name,
Status: "rebasing", Status: models.StatusRebasing,
} }
} }
@ -349,11 +349,11 @@ func (self *CommitLoader) setCommitMergedStatuses(refName string, commits []*mod
if strings.HasPrefix(ancestor, commit.Sha) { if strings.HasPrefix(ancestor, commit.Sha) {
passedAncestor = true passedAncestor = true
} }
if commit.Status != "pushed" { if commit.Status != models.StatusPushed {
continue continue
} }
if passedAncestor { if passedAncestor {
commits[i].Status = "merged" commits[i].Status = models.StatusMerged
} }
} }
return commits, nil return commits, nil

View File

@ -78,7 +78,7 @@ func TestGetCommits(t *testing.T) {
{ {
Sha: "0eea75e8c631fba6b58135697835d58ba4c18dbc", Sha: "0eea75e8c631fba6b58135697835d58ba4c18dbc",
Name: "better typing for rebase mode", Name: "better typing for rebase mode",
Status: "unpushed", Status: models.StatusUnpushed,
Action: "", Action: "",
Tags: []string{}, Tags: []string{},
ExtraInfo: "(HEAD -> better-tests)", ExtraInfo: "(HEAD -> better-tests)",
@ -92,7 +92,7 @@ func TestGetCommits(t *testing.T) {
{ {
Sha: "b21997d6b4cbdf84b149d8e6a2c4d06a8e9ec164", Sha: "b21997d6b4cbdf84b149d8e6a2c4d06a8e9ec164",
Name: "fix logging", Name: "fix logging",
Status: "pushed", Status: models.StatusPushed,
Action: "", Action: "",
Tags: []string{}, Tags: []string{},
ExtraInfo: "(origin/better-tests)", ExtraInfo: "(origin/better-tests)",
@ -106,7 +106,7 @@ func TestGetCommits(t *testing.T) {
{ {
Sha: "e94e8fc5b6fab4cb755f29f1bdb3ee5e001df35c", Sha: "e94e8fc5b6fab4cb755f29f1bdb3ee5e001df35c",
Name: "refactor", Name: "refactor",
Status: "pushed", Status: models.StatusPushed,
Action: "", Action: "",
Tags: []string{}, Tags: []string{},
ExtraInfo: "", ExtraInfo: "",
@ -120,7 +120,7 @@ func TestGetCommits(t *testing.T) {
{ {
Sha: "d8084cd558925eb7c9c38afeed5725c21653ab90", Sha: "d8084cd558925eb7c9c38afeed5725c21653ab90",
Name: "WIP", Name: "WIP",
Status: "pushed", Status: models.StatusPushed,
Action: "", Action: "",
Tags: []string{}, Tags: []string{},
ExtraInfo: "", ExtraInfo: "",
@ -134,7 +134,7 @@ func TestGetCommits(t *testing.T) {
{ {
Sha: "65f910ebd85283b5cce9bf67d03d3f1a9ea3813a", Sha: "65f910ebd85283b5cce9bf67d03d3f1a9ea3813a",
Name: "WIP", Name: "WIP",
Status: "pushed", Status: models.StatusPushed,
Action: "", Action: "",
Tags: []string{}, Tags: []string{},
ExtraInfo: "", ExtraInfo: "",
@ -148,7 +148,7 @@ func TestGetCommits(t *testing.T) {
{ {
Sha: "26c07b1ab33860a1a7591a0638f9925ccf497ffa", Sha: "26c07b1ab33860a1a7591a0638f9925ccf497ffa",
Name: "WIP", Name: "WIP",
Status: "merged", Status: models.StatusMerged,
Action: "", Action: "",
Tags: []string{}, Tags: []string{},
ExtraInfo: "", ExtraInfo: "",
@ -162,7 +162,7 @@ func TestGetCommits(t *testing.T) {
{ {
Sha: "3d4470a6c072208722e5ae9a54bcb9634959a1c5", Sha: "3d4470a6c072208722e5ae9a54bcb9634959a1c5",
Name: "WIP", Name: "WIP",
Status: "merged", Status: models.StatusMerged,
Action: "", Action: "",
Tags: []string{}, Tags: []string{},
ExtraInfo: "", ExtraInfo: "",
@ -176,7 +176,7 @@ func TestGetCommits(t *testing.T) {
{ {
Sha: "053a66a7be3da43aacdc7aa78e1fe757b82c4dd2", Sha: "053a66a7be3da43aacdc7aa78e1fe757b82c4dd2",
Name: "refactoring the config struct", Name: "refactoring the config struct",
Status: "merged", Status: models.StatusMerged,
Action: "", Action: "",
Tags: []string{}, Tags: []string{},
ExtraInfo: "", ExtraInfo: "",

View File

@ -52,7 +52,7 @@ func (self *ReflogCommitLoader) GetReflogCommits(lastReflogCommit *models.Commit
Sha: fields[0], Sha: fields[0],
Name: fields[2], Name: fields[2],
UnixTimestamp: int64(unixTimestamp), UnixTimestamp: int64(unixTimestamp),
Status: "reflog", Status: models.StatusReflog,
Parents: parents, Parents: parents,
} }

View File

@ -51,35 +51,35 @@ func TestGetReflogCommits(t *testing.T) {
{ {
Sha: "c3c4b66b64c97ffeecde", Sha: "c3c4b66b64c97ffeecde",
Name: "checkout: moving from A to B", Name: "checkout: moving from A to B",
Status: "reflog", Status: models.StatusReflog,
UnixTimestamp: 1643150483, UnixTimestamp: 1643150483,
Parents: []string{"51baa8c1"}, Parents: []string{"51baa8c1"},
}, },
{ {
Sha: "c3c4b66b64c97ffeecde", Sha: "c3c4b66b64c97ffeecde",
Name: "checkout: moving from B to A", Name: "checkout: moving from B to A",
Status: "reflog", Status: models.StatusReflog,
UnixTimestamp: 1643150483, UnixTimestamp: 1643150483,
Parents: []string{"51baa8c1"}, Parents: []string{"51baa8c1"},
}, },
{ {
Sha: "c3c4b66b64c97ffeecde", Sha: "c3c4b66b64c97ffeecde",
Name: "checkout: moving from A to B", Name: "checkout: moving from A to B",
Status: "reflog", Status: models.StatusReflog,
UnixTimestamp: 1643150483, UnixTimestamp: 1643150483,
Parents: []string{"51baa8c1"}, Parents: []string{"51baa8c1"},
}, },
{ {
Sha: "c3c4b66b64c97ffeecde", Sha: "c3c4b66b64c97ffeecde",
Name: "checkout: moving from master to A", Name: "checkout: moving from master to A",
Status: "reflog", Status: models.StatusReflog,
UnixTimestamp: 1643150483, UnixTimestamp: 1643150483,
Parents: []string{"51baa8c1"}, Parents: []string{"51baa8c1"},
}, },
{ {
Sha: "f4ddf2f0d4be4ccc7efa", Sha: "f4ddf2f0d4be4ccc7efa",
Name: "checkout: moving from A to master", Name: "checkout: moving from A to master",
Status: "reflog", Status: models.StatusReflog,
UnixTimestamp: 1643149435, UnixTimestamp: 1643149435,
Parents: []string{"51baa8c1"}, Parents: []string{"51baa8c1"},
}, },
@ -95,7 +95,7 @@ func TestGetReflogCommits(t *testing.T) {
lastReflogCommit: &models.Commit{ lastReflogCommit: &models.Commit{
Sha: "c3c4b66b64c97ffeecde", Sha: "c3c4b66b64c97ffeecde",
Name: "checkout: moving from B to A", Name: "checkout: moving from B to A",
Status: "reflog", Status: models.StatusReflog,
UnixTimestamp: 1643150483, UnixTimestamp: 1643150483,
Parents: []string{"51baa8c1"}, Parents: []string{"51baa8c1"},
}, },
@ -103,7 +103,7 @@ func TestGetReflogCommits(t *testing.T) {
{ {
Sha: "c3c4b66b64c97ffeecde", Sha: "c3c4b66b64c97ffeecde",
Name: "checkout: moving from A to B", Name: "checkout: moving from A to B",
Status: "reflog", Status: models.StatusReflog,
UnixTimestamp: 1643150483, UnixTimestamp: 1643150483,
Parents: []string{"51baa8c1"}, Parents: []string{"51baa8c1"},
}, },
@ -119,7 +119,7 @@ func TestGetReflogCommits(t *testing.T) {
lastReflogCommit: &models.Commit{ lastReflogCommit: &models.Commit{
Sha: "c3c4b66b64c97ffeecde", Sha: "c3c4b66b64c97ffeecde",
Name: "checkout: moving from B to A", Name: "checkout: moving from B to A",
Status: "reflog", Status: models.StatusReflog,
UnixTimestamp: 1643150483, UnixTimestamp: 1643150483,
Parents: []string{"51baa8c1"}, Parents: []string{"51baa8c1"},
}, },
@ -128,7 +128,7 @@ func TestGetReflogCommits(t *testing.T) {
{ {
Sha: "c3c4b66b64c97ffeecde", Sha: "c3c4b66b64c97ffeecde",
Name: "checkout: moving from A to B", Name: "checkout: moving from A to B",
Status: "reflog", Status: models.StatusReflog,
UnixTimestamp: 1643150483, UnixTimestamp: 1643150483,
Parents: []string{"51baa8c1"}, Parents: []string{"51baa8c1"},
}, },

View File

@ -9,11 +9,23 @@ import (
// Special commit hash for empty tree object // Special commit hash for empty tree object
const EmptyTreeCommitHash = "4b825dc642cb6eb9a060e54bf8d69288fbee4904" const EmptyTreeCommitHash = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
type CommitStatus int
const (
StatusNone CommitStatus = iota
StatusUnpushed
StatusPushed
StatusMerged
StatusRebasing
StatusSelected
StatusReflog
)
// Commit : A git commit // Commit : A git commit
type Commit struct { type Commit struct {
Sha string Sha string
Name string Name string
Status string // one of "unpushed", "pushed", "merged", "rebasing" or "selected" Status CommitStatus
Action string // one of "", "pick", "edit", "squash", "reword", "drop", "fixup" Action string // one of "", "pick", "edit", "squash", "reword", "drop", "fixup"
Tags []string Tags []string
ExtraInfo string // something like 'HEAD -> master, tag: v0.15.2' ExtraInfo string // something like 'HEAD -> master, tag: v0.15.2'

View File

@ -346,16 +346,17 @@ func getShaColor(
diffed := commit.Sha == diffName diffed := commit.Sha == diffName
shaColor := theme.DefaultTextColor shaColor := theme.DefaultTextColor
switch commit.Status { switch commit.Status {
case "unpushed": case models.StatusUnpushed:
shaColor = style.FgRed shaColor = style.FgRed
case "pushed": case models.StatusPushed:
shaColor = style.FgYellow shaColor = style.FgYellow
case "merged": case models.StatusMerged:
shaColor = style.FgGreen shaColor = style.FgGreen
case "rebasing": case models.StatusRebasing:
shaColor = style.FgBlue shaColor = style.FgBlue
case "reflog": case models.StatusReflog:
shaColor = style.FgBlue shaColor = style.FgBlue
default:
} }
if diffed { if diffed {