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

introduce Ref interface

This commit is contained in:
Ryooooooga 2022-03-26 22:18:08 +09:00 committed by Jesse Duffield
parent 30be50b641
commit 4835fc00b8
23 changed files with 85 additions and 179 deletions

View File

@ -22,6 +22,10 @@ func (b *Branch) RefName() string {
return b.Name
}
func (b *Branch) ParentRefName() string {
return b.RefName() + "^"
}
func (b *Branch) ID() string {
return b.RefName()
}

View File

@ -14,6 +14,10 @@ func (r *RemoteBranch) RefName() string {
return r.FullName()
}
func (r *RemoteBranch) ParentRefName() string {
return r.RefName() + "^"
}
func (r *RemoteBranch) ID() string {
return r.RefName()
}

View File

@ -12,6 +12,10 @@ func (s *StashEntry) RefName() string {
return fmt.Sprintf("stash@{%d}", s.Index)
}
func (s *StashEntry) ParentRefName() string {
return s.RefName() + "^"
}
func (s *StashEntry) ID() string {
return s.RefName()
}

View File

@ -9,6 +9,10 @@ func (t *Tag) RefName() string {
return t.Name
}
func (t *Tag) ParentRefName() string {
return t.RefName() + "^"
}
func (t *Tag) ID() string {
return t.RefName()
}

View File

@ -16,8 +16,9 @@ func (gui *Gui) commitFilesRenderToMain() error {
return nil
}
to := gui.State.Contexts.CommitFiles.GetRefName()
from, reverse := gui.State.Modes.Diffing.GetFromAndReverseArgsForDiff(to)
ref := gui.State.Contexts.CommitFiles.GetRef()
to := ref.RefName()
from, reverse := gui.State.Modes.Diffing.GetFromAndReverseArgsForDiff(ref.ParentRefName())
cmdObj := gui.git.WorkingTree.ShowFileDiffCmdObj(from, to, reverse, node.GetPath(), false)
task := NewRunPtyTask(cmdObj.GetCmd())
@ -39,8 +40,7 @@ func (gui *Gui) commitFilesRenderToMain() error {
func (gui *Gui) SwitchToCommitFilesContext(opts controllers.SwitchToCommitFilesContextOpts) error {
gui.State.Contexts.CommitFiles.SetSelectedLineIdx(0)
gui.State.Contexts.CommitFiles.SetRefName(opts.RefName)
gui.State.Contexts.CommitFiles.SetTitleRef(opts.RefDescription)
gui.State.Contexts.CommitFiles.SetRef(opts.Ref)
gui.State.Contexts.CommitFiles.SetCanRebase(opts.CanRebase)
gui.State.Contexts.CommitFiles.SetParentContext(opts.Context)
gui.State.Contexts.CommitFiles.SetWindowName(opts.Context.GetWindowName())
@ -53,8 +53,9 @@ func (gui *Gui) SwitchToCommitFilesContext(opts controllers.SwitchToCommitFilesC
}
func (gui *Gui) refreshCommitFilesContext() error {
to := gui.State.Contexts.CommitFiles.GetRefName()
from, reverse := gui.State.Modes.Diffing.GetFromAndReverseArgsForDiff(to)
ref := gui.State.Contexts.CommitFiles.GetRef()
to := ref.RefName()
from, reverse := gui.State.Modes.Diffing.GetFromAndReverseArgsForDiff(ref.ParentRefName())
files, err := gui.git.Loaders.CommitFiles.GetFilesInDiff(from, to, reverse)
if err != nil {

View File

@ -57,21 +57,6 @@ func (self *BranchesContext) GetSelectedItemId() string {
return item.ID()
}
func (self *BranchesContext) GetSelectedRefName() string {
item := self.GetSelected()
if item == nil {
return ""
}
return item.RefName()
}
func (self *BranchesContext) GetSelectedDescription() string {
item := self.GetSelected()
if item == nil {
return ""
}
return item.Description()
func (self *BranchesContext) GetSelectedRef() types.Ref {
return self.GetSelected()
}

View File

@ -83,24 +83,8 @@ func (self *LocalCommitsContext) CanRebase() bool {
return true
}
func (self *LocalCommitsContext) GetSelectedRefName() string {
item := self.GetSelected()
if item == nil {
return ""
}
return item.RefName()
}
func (self *LocalCommitsViewModel) GetSelectedDescription() string {
item := self.GetSelected()
if item == nil {
return ""
}
return item.Description()
func (self *LocalCommitsContext) GetSelectedRef() types.Ref {
return self.GetSelected()
}
func (self *LocalCommitsViewModel) SetLimitCommits(value bool) {

View File

@ -61,26 +61,10 @@ func (self *ReflogCommitsContext) CanRebase() bool {
return false
}
func (self *ReflogCommitsContext) GetSelectedRefName() string {
item := self.GetSelected()
if item == nil {
return ""
}
return item.RefName()
func (self *ReflogCommitsContext) GetSelectedRef() types.Ref {
return self.GetSelected()
}
func (self *ReflogCommitsContext) GetCommits() []*models.Commit {
return self.getModel()
}
func (self *ReflogCommitsContext) GetSelectedDescription() string {
item := self.GetSelected()
if item == nil {
return ""
}
return item.Description()
}

View File

@ -60,21 +60,6 @@ func (self *RemoteBranchesContext) GetSelectedItemId() string {
return item.ID()
}
func (self *RemoteBranchesContext) GetSelectedRefName() string {
item := self.GetSelected()
if item == nil {
return ""
}
return item.RefName()
}
func (self *RemoteBranchesContext) GetSelectedDescription() string {
item := self.GetSelected()
if item == nil {
return ""
}
return item.Description()
func (self *RemoteBranchesContext) GetSelectedRef() types.Ref {
return self.GetSelected()
}

View File

@ -61,22 +61,6 @@ func (self *StashContext) CanRebase() bool {
return false
}
func (self *StashContext) GetSelectedRefName() string {
item := self.GetSelected()
if item == nil {
return ""
}
return item.RefName()
}
func (self *StashContext) GetSelectedDescription() string {
item := self.GetSelected()
if item == nil {
return ""
}
return item.Description()
func (self *StashContext) GetSelectedRef() types.Ref {
return self.GetSelected()
}

View File

@ -82,16 +82,8 @@ func (self *SubCommitsContext) CanRebase() bool {
return false
}
// not to be confused with the refName in the view model. This is the ref name of
// the selected commit
func (self *SubCommitsContext) GetSelectedRefName() string {
item := self.GetSelected()
if item == nil {
return ""
}
return item.RefName()
func (self *SubCommitsContext) GetSelectedRef() types.Ref {
return self.GetSelected()
}
func (self *SubCommitsContext) GetCommits() []*models.Commit {
@ -101,13 +93,3 @@ func (self *SubCommitsContext) GetCommits() []*models.Commit {
func (self *SubCommitsContext) Title() string {
return fmt.Sprintf(self.c.Tr.SubCommitsDynamicTitle, utils.TruncateWithEllipsis(self.refName, 50))
}
func (self *SubCommitsContext) GetSelectedDescription() string {
item := self.GetSelected()
if item == nil {
return ""
}
return item.Description()
}

View File

@ -57,21 +57,6 @@ func (self *TagsContext) GetSelectedItemId() string {
return item.ID()
}
func (self *TagsContext) GetSelectedRefName() string {
item := self.GetSelected()
if item == nil {
return ""
}
return item.RefName()
}
func (self *TagsContext) GetSelectedDescription() string {
item := self.GetSelected()
if item == nil {
return ""
}
return item.Description()
func (self *TagsContext) GetSelectedRef() types.Ref {
return self.GetSelected()
}

View File

@ -112,7 +112,7 @@ func (self *CommitFilesController) onClickMain(opts gocui.ViewMouseBindingOpts)
func (self *CommitFilesController) checkout(node *filetree.CommitFileNode) error {
self.c.LogAction(self.c.Tr.Actions.CheckoutFile)
if err := self.git.WorkingTree.CheckoutFile(self.context().GetRefName(), node.GetPath()); err != nil {
if err := self.git.WorkingTree.CheckoutFile(self.context().GetRef().RefName(), node.GetPath()); err != nil {
return self.c.Error(err)
}
@ -166,7 +166,7 @@ func (self *CommitFilesController) toggleForPatch(node *filetree.CommitFileNode)
// if there is any file that hasn't been fully added we'll fully add everything,
// otherwise we'll remove everything
adding := node.AnyFile(func(file *models.CommitFile) bool {
return self.git.Patch.PatchManager.GetFileStatus(file.Name, self.context().GetRefName()) != patch.WHOLE
return self.git.Patch.PatchManager.GetFileStatus(file.Name, self.context().GetRef().RefName()) != patch.WHOLE
})
err := node.ForEachFile(func(file *models.CommitFile) error {
@ -188,7 +188,7 @@ func (self *CommitFilesController) toggleForPatch(node *filetree.CommitFileNode)
})
}
if self.git.Patch.PatchManager.Active() && self.git.Patch.PatchManager.To != self.context().GetRefName() {
if self.git.Patch.PatchManager.Active() && self.git.Patch.PatchManager.To != self.context().GetRef().RefName() {
return self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.DiscardPatch,
Prompt: self.c.Tr.DiscardPatchConfirm,
@ -212,9 +212,9 @@ func (self *CommitFilesController) startPatchManager() error {
commitFilesContext := self.context()
canRebase := commitFilesContext.GetCanRebase()
to := commitFilesContext.GetRefName()
from, reverse := self.modes.Diffing.GetFromAndReverseArgsForDiff(to)
ref := commitFilesContext.GetRef()
to := ref.RefName()
from, reverse := self.modes.Diffing.GetFromAndReverseArgsForDiff(ref.ParentRefName())
self.git.Patch.PatchManager.Start(from, to, reverse, canRebase)
return nil
@ -239,7 +239,7 @@ func (self *CommitFilesController) enterCommitFile(node *filetree.CommitFileNode
return self.c.PushContext(self.contexts.PatchBuilding, opts)
}
if self.git.Patch.PatchManager.Active() && self.git.Patch.PatchManager.To != self.context().GetRefName() {
if self.git.Patch.PatchManager.Active() && self.git.Patch.PatchManager.To != self.context().GetRef().RefName() {
return self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.DiscardPatch,
Prompt: self.c.Tr.DiscardPatchConfirm,

View File

@ -11,8 +11,7 @@ var _ types.IController = &SwitchToDiffFilesController{}
type CanSwitchToDiffFiles interface {
types.Context
CanRebase() bool
GetSelectedRefName() string
GetSelectedDescription() string
GetSelectedRef() types.Ref
}
type SwitchToDiffFilesController struct {
@ -51,23 +50,22 @@ func (self *SwitchToDiffFilesController) GetOnClick() func() error {
return self.checkSelected(self.enter)
}
func (self *SwitchToDiffFilesController) checkSelected(callback func(string) error) func() error {
func (self *SwitchToDiffFilesController) checkSelected(callback func(types.Ref) error) func() error {
return func() error {
refName := self.context.GetSelectedRefName()
if refName == "" {
ref := self.context.GetSelectedRef()
if ref == nil {
return nil
}
return callback(refName)
return callback(ref)
}
}
func (self *SwitchToDiffFilesController) enter(refName string) error {
func (self *SwitchToDiffFilesController) enter(ref types.Ref) error {
return self.viewFiles(SwitchToCommitFilesContextOpts{
RefName: refName,
RefDescription: self.context.GetSelectedDescription(),
CanRebase: self.context.CanRebase(),
Context: self.context,
Ref: ref,
CanRebase: self.context.CanRebase(),
Context: self.context,
})
}

View File

@ -10,8 +10,7 @@ var _ types.IController = &SwitchToSubCommitsController{}
type CanSwitchToSubCommits interface {
types.Context
GetSelectedRefName() string
GetSelectedDescription() string
GetSelectedRef() types.Ref
}
type SwitchToSubCommitsController struct {
@ -52,8 +51,8 @@ func (self *SwitchToSubCommitsController) GetOnClick() func() error {
}
func (self *SwitchToSubCommitsController) viewCommits() error {
refName := self.context.GetSelectedRefName()
if refName == "" {
ref := self.context.GetSelectedRef()
if ref == nil {
return nil
}
@ -63,7 +62,7 @@ func (self *SwitchToSubCommitsController) viewCommits() error {
Limit: true,
FilterPath: self.modes.Filtering.GetPath(),
IncludeRebaseCommits: false,
RefName: refName,
RefName: ref.RefName(),
},
)
if err != nil {
@ -75,8 +74,8 @@ func (self *SwitchToSubCommitsController) viewCommits() error {
self.contexts.SubCommits.SetSelectedLineIdx(0)
self.contexts.SubCommits.SetParentContext(self.context)
self.contexts.SubCommits.SetWindowName(self.context.GetWindowName())
self.contexts.SubCommits.SetTitleRef(self.context.GetSelectedDescription())
self.contexts.SubCommits.SetRefName(refName)
self.contexts.SubCommits.SetTitleRef(ref.Description())
self.contexts.SubCommits.SetRefName(ref.RefName())
err = self.c.PostRefreshUpdate(self.contexts.SubCommits)
if err != nil {

View File

@ -6,12 +6,8 @@ import (
// all fields mandatory (except `CanRebase` because it's boolean)
type SwitchToCommitFilesContextOpts struct {
// this is something like a commit sha or branch name
RefName string
// this will be displayed in the title of the view so we know whose diff files
// we're viewing
RefDescription string
// this is something like a commit or branch
Ref types.Ref
// from the local commits view we're allowed to do rebase stuff with any patch
// we generate from the diff files context, but we don't have that same ability

View File

@ -44,7 +44,7 @@ func (gui *Gui) currentDiffTerminals() []string {
// TODO: should we just return nil here?
return []string{""}
case *context.CommitFilesContext:
return []string{v.GetRefName()}
return []string{v.GetRef().RefName()}
case *context.BranchesContext:
// for our local branches we want to include both the branch and its upstream
branch := gui.State.Contexts.Branches.GetSelected()

View File

@ -13,8 +13,8 @@ type ICommitFileTreeViewModel interface {
ICommitFileTree
types.IListCursor
GetRefName() string
SetRefName(string)
GetRef() types.Ref
SetRef(types.Ref)
GetCanRebase() bool
SetCanRebase(bool)
}
@ -24,8 +24,8 @@ type CommitFileTreeViewModel struct {
ICommitFileTree
types.IListCursor
// this is e.g. the commit SHA of the commit for which we're viewing the files
refName string
// this is e.g. the commit for which we're viewing the files
ref types.Ref
// we set this to true when you're viewing the files within the checked-out branch's commits.
// If you're viewing the files of some random other branch we can't do any rebase stuff.
@ -40,17 +40,17 @@ func NewCommitFileTreeViewModel(getFiles func() []*models.CommitFile, log *logru
return &CommitFileTreeViewModel{
ICommitFileTree: fileTree,
IListCursor: listCursor,
refName: "",
ref: nil,
canRebase: false,
}
}
func (self *CommitFileTreeViewModel) GetRefName() string {
return self.refName
func (self *CommitFileTreeViewModel) GetRef() types.Ref {
return self.ref
}
func (self *CommitFileTreeViewModel) SetRefName(refName string) {
self.refName = refName
func (self *CommitFileTreeViewModel) SetRef(ref types.Ref) {
self.ref = ref
}
func (self *CommitFileTreeViewModel) GetCanRebase() bool {

View File

@ -16,8 +16,7 @@ func (self *Diffing) Active() bool {
// GetFromAndReverseArgsForDiff tells us the from and reverse args to be used in a diff command.
// If we're not in diff mode we'll end up with the equivalent of a `git show` i.e `git diff blah^..blah`.
func (self *Diffing) GetFromAndReverseArgsForDiff(to string) (string, bool) {
from := to + "^"
func (self *Diffing) GetFromAndReverseArgsForDiff(from string) (string, bool) {
reverse := false
if self.Active() {

View File

@ -16,8 +16,9 @@ func (gui *Gui) refreshPatchBuildingPanel(selectedLineIdx int) error {
return nil
}
to := gui.State.Contexts.CommitFiles.CommitFileTreeViewModel.GetRefName()
from, reverse := gui.State.Modes.Diffing.GetFromAndReverseArgsForDiff(to)
ref := gui.State.Contexts.CommitFiles.CommitFileTreeViewModel.GetRef()
to := ref.RefName()
from, reverse := gui.State.Modes.Diffing.GetFromAndReverseArgsForDiff(ref.ParentRefName())
diff, err := gui.git.WorkingTree.ShowFileDiff(from, to, reverse, node.GetPath(), true)
if err != nil {
return err

View File

@ -49,11 +49,11 @@ func RenderCommitFileTree(
// based on the leaves of that subtree
var status patch.PatchStatus
if castN.EveryFile(func(file *models.CommitFile) bool {
return patchManager.GetFileStatus(file.Name, tree.GetRefName()) == patch.WHOLE
return patchManager.GetFileStatus(file.Name, tree.GetRef().RefName()) == patch.WHOLE
}) {
status = patch.WHOLE
} else if castN.EveryFile(func(file *models.CommitFile) bool {
return patchManager.GetFileStatus(file.Name, tree.GetRefName()) == patch.UNSELECTED
return patchManager.GetFileStatus(file.Name, tree.GetRef().RefName()) == patch.UNSELECTED
}) {
status = patch.UNSELECTED
} else {

View File

@ -186,7 +186,7 @@ func (gui *Gui) refreshCommits() {
// For now the awkwardness remains.
commit := gui.getSelectedLocalCommit()
if commit != nil {
gui.State.Contexts.CommitFiles.SetRefName(commit.RefName())
gui.State.Contexts.CommitFiles.SetRef(commit)
gui.State.Contexts.CommitFiles.SetTitleRef(commit.RefName())
_ = gui.refreshCommitFilesContext()
}

7
pkg/gui/types/ref.go Normal file
View File

@ -0,0 +1,7 @@
package types
type Ref interface {
RefName() string
ParentRefName() string
Description() string
}