2022-02-05 08:04:10 +02:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
2023-03-23 03:02:03 +02:00
|
|
|
"log"
|
2023-05-26 08:38:58 +02:00
|
|
|
"time"
|
2023-03-23 03:02:03 +02:00
|
|
|
|
2022-02-05 08:04:10 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
2023-03-23 03:02:03 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
|
2022-02-05 08:04:10 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LocalCommitsContext struct {
|
|
|
|
*LocalCommitsViewModel
|
2023-05-25 09:09:18 +02:00
|
|
|
*ListContextTrait
|
2023-05-27 06:14:43 +02:00
|
|
|
*SearchTrait
|
2022-02-05 08:04:10 +02:00
|
|
|
}
|
|
|
|
|
2022-12-30 14:24:24 +02:00
|
|
|
var (
|
|
|
|
_ types.IListContext = (*LocalCommitsContext)(nil)
|
|
|
|
_ types.DiffableContext = (*LocalCommitsContext)(nil)
|
|
|
|
)
|
2022-02-05 08:04:10 +02:00
|
|
|
|
2023-03-23 03:35:07 +02:00
|
|
|
func NewLocalCommitsContext(c *ContextCommon) *LocalCommitsContext {
|
2023-03-21 12:16:30 +02:00
|
|
|
viewModel := NewLocalCommitsViewModel(
|
|
|
|
func() []*models.Commit { return c.Model().Commits },
|
|
|
|
c,
|
|
|
|
)
|
2022-02-05 08:04:10 +02:00
|
|
|
|
2023-03-23 03:02:03 +02:00
|
|
|
getDisplayStrings := func(startIdx int, length int) [][]string {
|
|
|
|
selectedCommitSha := ""
|
|
|
|
|
|
|
|
if c.CurrentContext().GetKey() == LOCAL_COMMITS_CONTEXT_KEY {
|
|
|
|
selectedCommit := viewModel.GetSelected()
|
|
|
|
if selectedCommit != nil {
|
|
|
|
selectedCommitSha = selectedCommit.Sha
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
showYouAreHereLabel := c.Model().WorkingTreeStateAtLastCommitRefresh == enums.REBASE_MODE_REBASING
|
|
|
|
|
|
|
|
return presentation.GetCommitListDisplayStrings(
|
|
|
|
c.Common,
|
|
|
|
c.Model().Commits,
|
|
|
|
c.State().GetRepoState().GetScreenMode() != types.SCREEN_NORMAL,
|
|
|
|
c.Modes().CherryPicking.SelectedShaSet(),
|
|
|
|
c.Modes().Diffing.Ref,
|
|
|
|
c.UserConfig.Gui.TimeFormat,
|
2023-05-26 08:38:58 +02:00
|
|
|
c.UserConfig.Gui.ShortTimeFormat,
|
|
|
|
time.Now(),
|
2023-03-23 03:02:03 +02:00
|
|
|
c.UserConfig.Git.ParseEmoji,
|
|
|
|
selectedCommitSha,
|
|
|
|
startIdx,
|
|
|
|
length,
|
|
|
|
shouldShowGraph(c),
|
|
|
|
c.Model().BisectInfo,
|
|
|
|
showYouAreHereLabel,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-05-27 06:14:43 +02:00
|
|
|
ctx := &LocalCommitsContext{
|
2022-02-05 08:04:10 +02:00
|
|
|
LocalCommitsViewModel: viewModel,
|
2023-05-27 06:14:43 +02:00
|
|
|
SearchTrait: NewSearchTrait(c),
|
2023-05-25 09:09:18 +02:00
|
|
|
ListContextTrait: &ListContextTrait{
|
|
|
|
Context: NewSimpleContext(NewBaseContext(NewBaseContextOpts{
|
|
|
|
View: c.Views().Commits,
|
|
|
|
WindowName: "commits",
|
|
|
|
Key: LOCAL_COMMITS_CONTEXT_KEY,
|
|
|
|
Kind: types.SIDE_CONTEXT,
|
|
|
|
Focusable: true,
|
|
|
|
})),
|
2023-06-01 11:28:26 +02:00
|
|
|
list: viewModel,
|
|
|
|
getDisplayStrings: getDisplayStrings,
|
|
|
|
c: c,
|
|
|
|
refreshViewportOnChange: true,
|
2022-03-19 00:38:49 +02:00
|
|
|
},
|
2022-02-05 08:04:10 +02:00
|
|
|
}
|
2023-05-27 06:14:43 +02:00
|
|
|
|
|
|
|
ctx.GetView().SetOnSelectItem(ctx.SearchTrait.onSelectItemWrapper(func(selectedLineIdx int) error {
|
|
|
|
ctx.GetList().SetSelectedLineIdx(selectedLineIdx)
|
|
|
|
return ctx.HandleFocus(types.OnFocusOpts{})
|
|
|
|
}))
|
|
|
|
|
|
|
|
return ctx
|
2022-02-05 08:04:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *LocalCommitsContext) GetSelectedItemId() string {
|
|
|
|
item := self.GetSelected()
|
|
|
|
if item == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return item.ID()
|
|
|
|
}
|
|
|
|
|
|
|
|
type LocalCommitsViewModel struct {
|
2023-05-27 06:14:43 +02:00
|
|
|
*ListViewModel[*models.Commit]
|
2022-02-06 06:54:26 +02:00
|
|
|
|
|
|
|
// If this is true we limit the amount of commits we load, for the sake of keeping things fast.
|
|
|
|
// If the user attempts to scroll past the end of the list, we will load more commits.
|
2022-02-06 05:37:16 +02:00
|
|
|
limitCommits bool
|
2022-02-06 06:54:26 +02:00
|
|
|
|
|
|
|
// If this is true we'll use git log --all when fetching the commits.
|
|
|
|
showWholeGitGraph bool
|
2022-02-05 08:04:10 +02:00
|
|
|
}
|
|
|
|
|
2023-03-23 03:35:07 +02:00
|
|
|
func NewLocalCommitsViewModel(getModel func() []*models.Commit, c *ContextCommon) *LocalCommitsViewModel {
|
2022-02-05 08:04:10 +02:00
|
|
|
self := &LocalCommitsViewModel{
|
2023-05-27 06:14:43 +02:00
|
|
|
ListViewModel: NewListViewModel(getModel),
|
2022-05-30 17:19:48 +02:00
|
|
|
limitCommits: true,
|
2022-05-30 07:52:39 +02:00
|
|
|
showWholeGitGraph: c.UserConfig.Git.Log.ShowWholeGraph,
|
2022-02-05 08:04:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
2022-02-13 08:54:36 +02:00
|
|
|
func (self *LocalCommitsContext) CanRebase() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-03-26 15:18:08 +02:00
|
|
|
func (self *LocalCommitsContext) GetSelectedRef() types.Ref {
|
2022-05-05 14:41:44 +02:00
|
|
|
commit := self.GetSelected()
|
|
|
|
if commit == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return commit
|
2022-03-26 05:44:30 +02:00
|
|
|
}
|
|
|
|
|
2022-12-30 14:24:24 +02:00
|
|
|
func (self *LocalCommitsContext) GetDiffTerminals() []string {
|
|
|
|
itemId := self.GetSelectedItemId()
|
|
|
|
|
|
|
|
return []string{itemId}
|
|
|
|
}
|
|
|
|
|
2022-02-06 05:37:16 +02:00
|
|
|
func (self *LocalCommitsViewModel) SetLimitCommits(value bool) {
|
|
|
|
self.limitCommits = value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *LocalCommitsViewModel) GetLimitCommits() bool {
|
|
|
|
return self.limitCommits
|
|
|
|
}
|
2022-02-06 06:54:26 +02:00
|
|
|
|
|
|
|
func (self *LocalCommitsViewModel) SetShowWholeGitGraph(value bool) {
|
|
|
|
self.showWholeGitGraph = value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *LocalCommitsViewModel) GetShowWholeGitGraph() bool {
|
|
|
|
return self.showWholeGitGraph
|
|
|
|
}
|
2022-03-26 07:42:56 +02:00
|
|
|
|
|
|
|
func (self *LocalCommitsViewModel) GetCommits() []*models.Commit {
|
|
|
|
return self.getModel()
|
|
|
|
}
|
2023-03-23 03:02:03 +02:00
|
|
|
|
2023-03-23 03:35:07 +02:00
|
|
|
func shouldShowGraph(c *ContextCommon) bool {
|
2023-03-23 03:02:03 +02:00
|
|
|
if c.Modes().Filtering.Active() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
value := c.UserConfig.Git.Log.ShowGraph
|
|
|
|
switch value {
|
|
|
|
case "always":
|
|
|
|
return true
|
|
|
|
case "never":
|
|
|
|
return false
|
|
|
|
case "when-maximised":
|
|
|
|
return c.State().GetRepoState().GetScreenMode() != types.SCREEN_NORMAL
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Fatalf("Unknown value for git.log.showGraph: %s. Expected one of: 'always', 'never', 'when-maximised'", value)
|
|
|
|
return false
|
|
|
|
}
|