1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-31 14:24:25 +03: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 return b.Name
} }
func (b *Branch) ParentRefName() string {
return b.RefName() + "^"
}
func (b *Branch) ID() string { func (b *Branch) ID() string {
return b.RefName() return b.RefName()
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -83,24 +83,8 @@ func (self *LocalCommitsContext) CanRebase() bool {
return true return true
} }
func (self *LocalCommitsContext) GetSelectedRefName() string { func (self *LocalCommitsContext) GetSelectedRef() types.Ref {
item := self.GetSelected() return 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 *LocalCommitsViewModel) SetLimitCommits(value bool) { func (self *LocalCommitsViewModel) SetLimitCommits(value bool) {

View File

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

View File

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

View File

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

View File

@ -112,7 +112,7 @@ func (self *CommitFilesController) onClickMain(opts gocui.ViewMouseBindingOpts)
func (self *CommitFilesController) checkout(node *filetree.CommitFileNode) error { func (self *CommitFilesController) checkout(node *filetree.CommitFileNode) error {
self.c.LogAction(self.c.Tr.Actions.CheckoutFile) 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) 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, // if there is any file that hasn't been fully added we'll fully add everything,
// otherwise we'll remove everything // otherwise we'll remove everything
adding := node.AnyFile(func(file *models.CommitFile) bool { 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 { 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{ return self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.DiscardPatch, Title: self.c.Tr.DiscardPatch,
Prompt: self.c.Tr.DiscardPatchConfirm, Prompt: self.c.Tr.DiscardPatchConfirm,
@ -212,9 +212,9 @@ func (self *CommitFilesController) startPatchManager() error {
commitFilesContext := self.context() commitFilesContext := self.context()
canRebase := commitFilesContext.GetCanRebase() canRebase := commitFilesContext.GetCanRebase()
to := commitFilesContext.GetRefName() ref := commitFilesContext.GetRef()
to := ref.RefName()
from, reverse := self.modes.Diffing.GetFromAndReverseArgsForDiff(to) from, reverse := self.modes.Diffing.GetFromAndReverseArgsForDiff(ref.ParentRefName())
self.git.Patch.PatchManager.Start(from, to, reverse, canRebase) self.git.Patch.PatchManager.Start(from, to, reverse, canRebase)
return nil return nil
@ -239,7 +239,7 @@ func (self *CommitFilesController) enterCommitFile(node *filetree.CommitFileNode
return self.c.PushContext(self.contexts.PatchBuilding, opts) 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{ return self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.DiscardPatch, Title: self.c.Tr.DiscardPatch,
Prompt: self.c.Tr.DiscardPatchConfirm, Prompt: self.c.Tr.DiscardPatchConfirm,

View File

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

View File

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

View File

@ -6,12 +6,8 @@ import (
// all fields mandatory (except `CanRebase` because it's boolean) // all fields mandatory (except `CanRebase` because it's boolean)
type SwitchToCommitFilesContextOpts struct { type SwitchToCommitFilesContextOpts struct {
// this is something like a commit sha or branch name // this is something like a commit or branch
RefName string Ref types.Ref
// this will be displayed in the title of the view so we know whose diff files
// we're viewing
RefDescription string
// from the local commits view we're allowed to do rebase stuff with any patch // 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 // 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? // TODO: should we just return nil here?
return []string{""} return []string{""}
case *context.CommitFilesContext: case *context.CommitFilesContext:
return []string{v.GetRefName()} return []string{v.GetRef().RefName()}
case *context.BranchesContext: case *context.BranchesContext:
// for our local branches we want to include both the branch and its upstream // for our local branches we want to include both the branch and its upstream
branch := gui.State.Contexts.Branches.GetSelected() branch := gui.State.Contexts.Branches.GetSelected()

View File

@ -13,8 +13,8 @@ type ICommitFileTreeViewModel interface {
ICommitFileTree ICommitFileTree
types.IListCursor types.IListCursor
GetRefName() string GetRef() types.Ref
SetRefName(string) SetRef(types.Ref)
GetCanRebase() bool GetCanRebase() bool
SetCanRebase(bool) SetCanRebase(bool)
} }
@ -24,8 +24,8 @@ type CommitFileTreeViewModel struct {
ICommitFileTree ICommitFileTree
types.IListCursor types.IListCursor
// this is e.g. the commit SHA of the commit for which we're viewing the files // this is e.g. the commit for which we're viewing the files
refName string ref types.Ref
// we set this to true when you're viewing the files within the checked-out branch's commits. // 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. // 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{ return &CommitFileTreeViewModel{
ICommitFileTree: fileTree, ICommitFileTree: fileTree,
IListCursor: listCursor, IListCursor: listCursor,
refName: "", ref: nil,
canRebase: false, canRebase: false,
} }
} }
func (self *CommitFileTreeViewModel) GetRefName() string { func (self *CommitFileTreeViewModel) GetRef() types.Ref {
return self.refName return self.ref
} }
func (self *CommitFileTreeViewModel) SetRefName(refName string) { func (self *CommitFileTreeViewModel) SetRef(ref types.Ref) {
self.refName = refName self.ref = ref
} }
func (self *CommitFileTreeViewModel) GetCanRebase() bool { 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. // 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`. // 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) { func (self *Diffing) GetFromAndReverseArgsForDiff(from string) (string, bool) {
from := to + "^"
reverse := false reverse := false
if self.Active() { if self.Active() {

View File

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

View File

@ -49,11 +49,11 @@ func RenderCommitFileTree(
// based on the leaves of that subtree // based on the leaves of that subtree
var status patch.PatchStatus var status patch.PatchStatus
if castN.EveryFile(func(file *models.CommitFile) bool { 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 status = patch.WHOLE
} else if castN.EveryFile(func(file *models.CommitFile) bool { } 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 status = patch.UNSELECTED
} else { } else {

View File

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