1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/presentation/commits.go

178 lines
4.2 KiB
Go
Raw Normal View History

2020-02-25 11:11:07 +02:00
package presentation
import (
"strings"
2021-11-02 07:39:15 +02:00
"sync"
2020-02-25 11:11:07 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/presentation/authors"
2021-11-02 07:39:15 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/presentation/graph"
"github.com/jesseduffield/lazygit/pkg/gui/style"
2020-02-25 11:11:07 +02:00
"github.com/jesseduffield/lazygit/pkg/theme"
"github.com/jesseduffield/lazygit/pkg/utils"
2021-07-16 14:06:01 +02:00
"github.com/kyokomi/emoji/v2"
2020-02-25 11:11:07 +02:00
)
2021-11-02 07:39:15 +02:00
type pipeSetCacheKey struct {
commitSha string
commitCount int
}
2020-02-25 11:11:07 +02:00
2021-11-02 07:39:15 +02:00
var pipeSetCache = make(map[pipeSetCacheKey][][]*graph.Pipe)
var mutex sync.Mutex
func GetCommitListDisplayStrings(
commits []*models.Commit,
fullDescription bool,
cherryPickedCommitShaMap map[string]bool,
diffName string,
parseEmoji bool,
selectedCommitSha string,
startIdx int,
length int,
2021-11-02 11:05:23 +02:00
showGraph bool,
2021-11-02 07:39:15 +02:00
) [][]string {
mutex.Lock()
defer mutex.Unlock()
if len(commits) == 0 {
return nil
2020-02-25 11:11:07 +02:00
}
2021-11-02 07:39:15 +02:00
// given that our cache key is a commit sha and a commit count, it's very important that we don't actually try to render pipes
// when dealing with things like filtered commits.
cacheKey := pipeSetCacheKey{
commitSha: commits[0].Sha,
commitCount: len(commits),
2020-02-25 11:11:07 +02:00
}
2021-11-02 07:39:15 +02:00
pipeSets, ok := pipeSetCache[cacheKey]
if !ok {
// pipe sets are unique to a commit head. and a commit count. Sometimes we haven't loaded everything for that.
// so let's just cache it based on that.
getStyle := func(commit *models.Commit) style.TextStyle {
return authors.AuthorStyle(commit.Author)
}
pipeSets = graph.GetPipeSets(commits, getStyle)
pipeSetCache[cacheKey] = pipeSets
}
2021-11-02 12:33:22 +02:00
if startIdx > len(commits) {
return nil
}
2021-11-02 07:39:15 +02:00
end := startIdx + length
if end > len(commits)-1 {
end = len(commits) - 1
}
filteredCommits := commits[startIdx : end+1]
2021-11-02 11:05:23 +02:00
var getGraphLine func(int) string
if showGraph {
filteredPipeSets := pipeSets[startIdx : end+1]
graphLines := graph.RenderAux(filteredPipeSets, filteredCommits, selectedCommitSha)
getGraphLine = func(idx int) string { return graphLines[idx] }
} else {
getGraphLine = func(idx int) string { return "" }
}
lines := make([][]string, 0, len(filteredCommits))
2021-11-02 07:39:15 +02:00
for i, commit := range filteredCommits {
2021-11-02 11:05:23 +02:00
lines = append(lines, displayCommit(commit, cherryPickedCommitShaMap, diffName, parseEmoji, getGraphLine(i), fullDescription))
2021-11-02 07:39:15 +02:00
}
2020-02-25 11:11:07 +02:00
return lines
}
2021-11-02 07:39:15 +02:00
func displayCommit(
commit *models.Commit,
cherryPickedCommitShaMap map[string]bool,
diffName string,
parseEmoji bool,
graphLine string,
fullDescription bool,
) []string {
2020-02-25 11:11:07 +02:00
2021-11-02 07:39:15 +02:00
shaColor := getShaColor(commit, diffName, cherryPickedCommitShaMap)
actionString := ""
if commit.Action != "" {
actionString = actionColorMap(commit.Action).Sprint(commit.Action) + " "
2020-02-25 11:11:07 +02:00
}
tagString := ""
2021-11-02 07:39:15 +02:00
if fullDescription {
if commit.ExtraInfo != "" {
tagString = style.FgMagenta.SetBold().Sprint(commit.ExtraInfo) + " "
}
} else {
if len(commit.Tags) > 0 {
tagString = theme.DiffTerminalColor.SetBold().Sprint(strings.Join(commit.Tags, " ")) + " "
}
2020-02-25 11:11:07 +02:00
}
2021-11-02 07:39:15 +02:00
name := commit.Name
2021-07-16 14:06:01 +02:00
if parseEmoji {
name = emoji.Sprint(name)
}
2021-11-02 07:39:15 +02:00
authorFunc := authors.ShortAuthor
if fullDescription {
authorFunc = authors.LongAuthor
}
2021-11-02 07:39:15 +02:00
cols := make([]string, 0, 5)
cols = append(cols, shaColor.Sprint(commit.ShortSha()))
if fullDescription {
cols = append(cols, style.FgBlue.Sprint(utils.UnixToDate(commit.UnixTimestamp)))
}
cols = append(
cols,
actionString,
authorFunc(commit.Author),
graphLine+tagString+theme.DefaultTextColor.Sprint(name),
)
return cols
2020-02-25 11:11:07 +02:00
}
2021-11-02 07:39:15 +02:00
func getShaColor(commit *models.Commit, diffName string, cherryPickedCommitShaMap map[string]bool) style.TextStyle {
diffed := commit.Sha == diffName
shaColor := theme.DefaultTextColor
2021-11-02 07:39:15 +02:00
switch commit.Status {
2020-02-25 11:11:07 +02:00
case "unpushed":
shaColor = style.FgRed
2020-02-25 11:11:07 +02:00
case "pushed":
shaColor = style.FgYellow
2020-02-25 11:11:07 +02:00
case "merged":
shaColor = style.FgGreen
2020-02-25 11:11:07 +02:00
case "rebasing":
shaColor = style.FgBlue
2020-02-25 11:11:07 +02:00
case "reflog":
shaColor = style.FgBlue
2020-02-25 11:11:07 +02:00
}
if diffed {
shaColor = theme.DiffTerminalColor
2021-11-02 07:39:15 +02:00
} else if cherryPickedCommitShaMap[commit.Sha] {
shaColor = theme.CherryPickedCommitTextStyle
2020-02-25 11:11:07 +02:00
}
2021-11-02 07:39:15 +02:00
return shaColor
2020-02-25 11:11:07 +02:00
}
func actionColorMap(str string) style.TextStyle {
switch str {
case "pick":
return style.FgCyan
case "drop":
return style.FgRed
case "edit":
return style.FgGreen
case "fixup":
return style.FgMagenta
default:
return style.FgYellow
}
}