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

406 lines
10 KiB
Go
Raw Normal View History

2020-02-25 11:11:07 +02:00
package presentation
import (
"fmt"
2020-02-25 11:11:07 +02:00
"strings"
"time"
2020-02-25 11:11:07 +02:00
"github.com/fsmiamoto/git-todo-parser/todo"
2022-03-19 03:26:30 +02:00
"github.com/jesseduffield/generics/set"
2022-01-19 09:32:27 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/gui/presentation/authors"
2021-11-02 07:39:15 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/presentation/graph"
2022-04-23 03:41:40 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/presentation/icons"
"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"
"github.com/samber/lo"
"github.com/sasha-s/go-deadlock"
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
2022-03-19 00:38:49 +02:00
var (
pipeSetCache = make(map[pipeSetCacheKey][][]*graph.Pipe)
mutex deadlock.Mutex
2022-03-19 00:38:49 +02:00
)
2021-11-02 07:39:15 +02:00
type bisectBounds struct {
newIndex int
oldIndex int
}
2022-01-19 09:32:27 +02:00
2021-11-02 07:39:15 +02:00
func GetCommitListDisplayStrings(
common *common.Common,
2021-11-02 07:39:15 +02:00
commits []*models.Commit,
fullDescription bool,
2022-03-19 03:26:30 +02:00
cherryPickedCommitShaSet *set.Set[string],
2021-11-02 07:39:15 +02:00
diffName string,
timeFormat string,
shortTimeFormat string,
now time.Time,
2021-11-02 07:39:15 +02:00
parseEmoji bool,
selectedCommitSha string,
startIdx int,
length int,
2021-11-02 11:05:23 +02:00
showGraph bool,
2022-01-19 09:32:27 +02:00
bisectInfo *git_commands.BisectInfo,
showYouAreHereLabel 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 12:33:22 +02:00
if startIdx > len(commits) {
return nil
}
2021-11-02 07:39:15 +02:00
end := utils.Min(startIdx+length, len(commits))
2022-01-24 10:42:40 +02:00
// this is where my non-TODO commits begin
rebaseOffset := utils.Min(indexOfFirstNonTODOCommit(commits), end)
filteredCommits := commits[startIdx:end]
2021-11-02 07:39:15 +02:00
bisectBounds := getbisectBounds(commits, bisectInfo)
// function expects to be passed the index of the commit in terms of the `commits` slice
2021-11-02 11:05:23 +02:00
var getGraphLine func(int) string
if showGraph {
// this is where the graph begins (may be beyond the TODO commits depending on startIdx,
// but we'll never include TODO commits as part of the graph because it'll be messy)
graphOffset := utils.Max(startIdx, rebaseOffset)
pipeSets := loadPipesets(commits[rebaseOffset:])
pipeSetOffset := utils.Max(startIdx-rebaseOffset, 0)
graphPipeSets := pipeSets[pipeSetOffset:utils.Max(end-rebaseOffset, 0)]
graphCommits := commits[graphOffset:end]
graphLines := graph.RenderAux(
graphPipeSets,
graphCommits,
selectedCommitSha,
)
getGraphLine = func(idx int) string {
if idx >= graphOffset {
return graphLines[idx-graphOffset]
} else {
return ""
}
}
2021-11-02 11:05:23 +02:00
} else {
getGraphLine = func(idx int) string { return "" }
}
lines := make([][]string, 0, len(filteredCommits))
2022-01-19 09:32:27 +02:00
var bisectStatus BisectStatus
2021-11-02 07:39:15 +02:00
for i, commit := range filteredCommits {
unfilteredIdx := i + startIdx
bisectStatus = getBisectStatus(unfilteredIdx, commit.Sha, bisectInfo, bisectBounds)
isYouAreHereCommit := false
if showYouAreHereLabel && (commit.Action == models.ActionConflict || unfilteredIdx == rebaseOffset) {
isYouAreHereCommit = true
showYouAreHereLabel = false
}
2022-01-19 09:32:27 +02:00
lines = append(lines, displayCommit(
common,
2022-01-19 09:32:27 +02:00
commit,
2022-03-19 03:26:30 +02:00
cherryPickedCommitShaSet,
2022-01-19 09:32:27 +02:00
diffName,
timeFormat,
shortTimeFormat,
now,
2022-01-19 09:32:27 +02:00
parseEmoji,
getGraphLine(unfilteredIdx),
2022-01-19 09:32:27 +02:00
fullDescription,
bisectStatus,
bisectInfo,
isYouAreHereCommit,
2022-01-19 09:32:27 +02:00
))
2021-11-02 07:39:15 +02:00
}
2020-02-25 11:11:07 +02:00
return lines
}
func getbisectBounds(commits []*models.Commit, bisectInfo *git_commands.BisectInfo) *bisectBounds {
if !bisectInfo.Bisecting() {
return nil
}
bisectBounds := &bisectBounds{}
for i, commit := range commits {
if commit.Sha == bisectInfo.GetNewSha() {
bisectBounds.newIndex = i
}
status, ok := bisectInfo.Status(commit.Sha)
if ok && status == git_commands.BisectStatusOld {
bisectBounds.oldIndex = i
return bisectBounds
}
}
// shouldn't land here
return nil
}
// precondition: slice is not empty
func indexOfFirstNonTODOCommit(commits []*models.Commit) int {
for i, commit := range commits {
if !commit.IsTODO() {
return i
}
}
// shouldn't land here
return 0
}
func loadPipesets(commits []*models.Commit) [][]*graph.Pipe {
// 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),
}
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 {
2022-05-08 06:23:29 +02:00
return authors.AuthorStyle(commit.AuthorName)
}
pipeSets = graph.GetPipeSets(commits, getStyle)
pipeSetCache[cacheKey] = pipeSets
}
return pipeSets
}
2022-01-19 09:32:27 +02:00
// similar to the git_commands.BisectStatus but more gui-focused
type BisectStatus int
const (
BisectStatusNone BisectStatus = iota
BisectStatusOld
BisectStatusNew
BisectStatusSkipped
// adding candidate here which isn't present in the commands package because
// we need to actually go through the commits to get this info
BisectStatusCandidate
// also adding this
BisectStatusCurrent
)
func getBisectStatus(index int, commitSha string, bisectInfo *git_commands.BisectInfo, bisectBounds *bisectBounds) BisectStatus {
2022-01-19 09:32:27 +02:00
if !bisectInfo.Started() {
return BisectStatusNone
2022-01-19 09:32:27 +02:00
}
if bisectInfo.GetCurrentSha() == commitSha {
return BisectStatusCurrent
2022-01-19 09:32:27 +02:00
}
status, ok := bisectInfo.Status(commitSha)
if ok {
switch status {
case git_commands.BisectStatusNew:
return BisectStatusNew
2022-01-19 09:32:27 +02:00
case git_commands.BisectStatusOld:
return BisectStatusOld
2022-01-19 09:32:27 +02:00
case git_commands.BisectStatusSkipped:
return BisectStatusSkipped
2022-01-19 09:32:27 +02:00
}
} else {
if bisectBounds != nil && index >= bisectBounds.newIndex && index <= bisectBounds.oldIndex {
return BisectStatusCandidate
2022-01-19 09:32:27 +02:00
} else {
return BisectStatusNone
2022-01-19 09:32:27 +02:00
}
}
// should never land here
return BisectStatusNone
2022-01-19 09:32:27 +02:00
}
func getBisectStatusText(bisectStatus BisectStatus, bisectInfo *git_commands.BisectInfo) string {
if bisectStatus == BisectStatusNone {
return ""
}
style := getBisectStatusColor(bisectStatus)
switch bisectStatus {
case BisectStatusNew:
return style.Sprintf("<-- " + bisectInfo.NewTerm())
case BisectStatusOld:
return style.Sprintf("<-- " + bisectInfo.OldTerm())
case BisectStatusCurrent:
// TODO: i18n
return style.Sprintf("<-- current")
case BisectStatusSkipped:
return style.Sprintf("<-- skipped")
case BisectStatusCandidate:
return style.Sprintf("?")
2022-03-19 00:38:49 +02:00
case BisectStatusNone:
return ""
2022-01-19 09:32:27 +02:00
}
return ""
}
2021-11-02 07:39:15 +02:00
func displayCommit(
common *common.Common,
2021-11-02 07:39:15 +02:00
commit *models.Commit,
2022-03-19 03:26:30 +02:00
cherryPickedCommitShaSet *set.Set[string],
2021-11-02 07:39:15 +02:00
diffName string,
timeFormat string,
shortTimeFormat string,
now time.Time,
2021-11-02 07:39:15 +02:00
parseEmoji bool,
graphLine string,
fullDescription bool,
2022-01-19 09:32:27 +02:00
bisectStatus BisectStatus,
bisectInfo *git_commands.BisectInfo,
isYouAreHereCommit bool,
2021-11-02 07:39:15 +02:00
) []string {
2022-03-19 03:26:30 +02:00
shaColor := getShaColor(commit, diffName, cherryPickedCommitShaSet, bisectStatus, bisectInfo)
2022-01-19 09:32:27 +02:00
bisectString := getBisectStatusText(bisectStatus, bisectInfo)
2021-11-02 07:39:15 +02:00
actionString := ""
if commit.Action != models.ActionNone {
todoString := lo.Ternary(commit.Action == models.ActionConflict, "conflict", commit.Action.String())
actionString = actionColorMap(commit.Action).Sprint(todoString) + " "
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, " ")) + " "
} else if common.UserConfig.Gui.ExperimentalShowBranchHeads && commit.ExtraInfo != "" {
tagString = style.FgMagenta.SetBold().Sprint("(*)") + " "
2021-11-02 07:39:15 +02:00
}
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)
}
if isYouAreHereCommit {
color := lo.Ternary(commit.Action == models.ActionConflict, style.FgRed, style.FgYellow)
youAreHere := color.Sprintf("<-- %s ---", common.Tr.YouAreHere)
name = fmt.Sprintf("%s %s", youAreHere, 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
2022-04-23 03:41:40 +02:00
cols := make([]string, 0, 7)
if icons.IsIconEnabled() {
cols = append(cols, shaColor.Sprint(icons.IconForCommit(commit)))
}
2021-11-02 07:39:15 +02:00
cols = append(cols, shaColor.Sprint(commit.ShortSha()))
2022-01-19 09:32:27 +02:00
cols = append(cols, bisectString)
2021-11-02 07:39:15 +02:00
if fullDescription {
cols = append(cols, style.FgBlue.Sprint(
utils.UnixToDateSmart(now, commit.UnixTimestamp, timeFormat, shortTimeFormat),
))
2021-11-02 07:39:15 +02:00
}
cols = append(
cols,
actionString,
2022-05-08 06:23:29 +02:00
authorFunc(commit.AuthorName),
2021-11-02 07:39:15 +02:00
graphLine+tagString+theme.DefaultTextColor.Sprint(name),
)
return cols
2022-01-19 09:32:27 +02:00
}
func getBisectStatusColor(status BisectStatus) style.TextStyle {
switch status {
case BisectStatusNone:
return style.FgBlack
case BisectStatusNew:
return style.FgRed
case BisectStatusOld:
return style.FgGreen
case BisectStatusSkipped:
return style.FgYellow
case BisectStatusCurrent:
return style.FgMagenta
case BisectStatusCandidate:
return style.FgBlue
}
2021-11-02 07:39:15 +02:00
2022-01-19 09:32:27 +02:00
// shouldn't land here
return style.FgWhite
2020-02-25 11:11:07 +02:00
}
2022-01-19 09:32:27 +02:00
func getShaColor(
commit *models.Commit,
diffName string,
2022-03-19 03:26:30 +02:00
cherryPickedCommitShaSet *set.Set[string],
2022-01-19 09:32:27 +02:00
bisectStatus BisectStatus,
bisectInfo *git_commands.BisectInfo,
) style.TextStyle {
if bisectInfo.Started() {
return getBisectStatusColor(bisectStatus)
}
diffed := commit.Sha != "" && commit.Sha == diffName
shaColor := theme.DefaultTextColor
2021-11-02 07:39:15 +02:00
switch commit.Status {
case models.StatusUnpushed:
shaColor = style.FgRed
case models.StatusPushed:
shaColor = style.FgYellow
case models.StatusMerged:
shaColor = style.FgGreen
case models.StatusRebasing:
shaColor = style.FgBlue
case models.StatusReflog:
shaColor = style.FgBlue
default:
2020-02-25 11:11:07 +02:00
}
if diffed {
shaColor = theme.DiffTerminalColor
2022-03-19 03:26:30 +02:00
} else if cherryPickedCommitShaSet.Includes(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(action todo.TodoCommand) style.TextStyle {
switch action {
case todo.Pick:
return style.FgCyan
case todo.Drop:
return style.FgRed
case todo.Edit:
return style.FgGreen
case todo.Fixup:
return style.FgMagenta
case models.ActionConflict:
return style.FgRed
default:
return style.FgYellow
}
}