diff --git a/pkg/commands/models/branch.go b/pkg/commands/models/branch.go index dae934fdf..b41e157f3 100644 --- a/pkg/commands/models/branch.go +++ b/pkg/commands/models/branch.go @@ -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() } diff --git a/pkg/commands/models/remote_branch.go b/pkg/commands/models/remote_branch.go index bee004fdb..e36592749 100644 --- a/pkg/commands/models/remote_branch.go +++ b/pkg/commands/models/remote_branch.go @@ -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() } diff --git a/pkg/commands/models/stash_entry.go b/pkg/commands/models/stash_entry.go index efda6bc77..d15bfd2eb 100644 --- a/pkg/commands/models/stash_entry.go +++ b/pkg/commands/models/stash_entry.go @@ -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() } diff --git a/pkg/commands/models/tag.go b/pkg/commands/models/tag.go index 2fb024e66..60b53e659 100644 --- a/pkg/commands/models/tag.go +++ b/pkg/commands/models/tag.go @@ -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() } diff --git a/pkg/gui/commit_files_panel.go b/pkg/gui/commit_files_panel.go index 7bb78b4bc..89f424779 100644 --- a/pkg/gui/commit_files_panel.go +++ b/pkg/gui/commit_files_panel.go @@ -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 { diff --git a/pkg/gui/context/branches_context.go b/pkg/gui/context/branches_context.go index 302f0c1d9..e64a83137 100644 --- a/pkg/gui/context/branches_context.go +++ b/pkg/gui/context/branches_context.go @@ -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() } diff --git a/pkg/gui/context/local_commits_context.go b/pkg/gui/context/local_commits_context.go index 0d7cc2f54..c5fcc769a 100644 --- a/pkg/gui/context/local_commits_context.go +++ b/pkg/gui/context/local_commits_context.go @@ -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) { diff --git a/pkg/gui/context/reflog_commits_context.go b/pkg/gui/context/reflog_commits_context.go index 0274a921e..33c162aaf 100644 --- a/pkg/gui/context/reflog_commits_context.go +++ b/pkg/gui/context/reflog_commits_context.go @@ -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() -} diff --git a/pkg/gui/context/remote_branches_context.go b/pkg/gui/context/remote_branches_context.go index 52217ef11..affcedf97 100644 --- a/pkg/gui/context/remote_branches_context.go +++ b/pkg/gui/context/remote_branches_context.go @@ -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() } diff --git a/pkg/gui/context/stash_context.go b/pkg/gui/context/stash_context.go index ef443846f..53d854669 100644 --- a/pkg/gui/context/stash_context.go +++ b/pkg/gui/context/stash_context.go @@ -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() } diff --git a/pkg/gui/context/sub_commits_context.go b/pkg/gui/context/sub_commits_context.go index ffc053267..d9a1e7c6b 100644 --- a/pkg/gui/context/sub_commits_context.go +++ b/pkg/gui/context/sub_commits_context.go @@ -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() -} diff --git a/pkg/gui/context/tags_context.go b/pkg/gui/context/tags_context.go index d6f8d78ac..09285aa76 100644 --- a/pkg/gui/context/tags_context.go +++ b/pkg/gui/context/tags_context.go @@ -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() } diff --git a/pkg/gui/controllers/commits_files_controller.go b/pkg/gui/controllers/commits_files_controller.go index b28578822..20e78997e 100644 --- a/pkg/gui/controllers/commits_files_controller.go +++ b/pkg/gui/controllers/commits_files_controller.go @@ -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, diff --git a/pkg/gui/controllers/switch_to_diff_files_controller.go b/pkg/gui/controllers/switch_to_diff_files_controller.go index c41dbdd37..275a5ebb2 100644 --- a/pkg/gui/controllers/switch_to_diff_files_controller.go +++ b/pkg/gui/controllers/switch_to_diff_files_controller.go @@ -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, }) } diff --git a/pkg/gui/controllers/switch_to_sub_commits_controller.go b/pkg/gui/controllers/switch_to_sub_commits_controller.go index d59f4fdbf..b41d122ed 100644 --- a/pkg/gui/controllers/switch_to_sub_commits_controller.go +++ b/pkg/gui/controllers/switch_to_sub_commits_controller.go @@ -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 { diff --git a/pkg/gui/controllers/types.go b/pkg/gui/controllers/types.go index e9af41089..f719b5de0 100644 --- a/pkg/gui/controllers/types.go +++ b/pkg/gui/controllers/types.go @@ -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 diff --git a/pkg/gui/diffing.go b/pkg/gui/diffing.go index daa659f7f..6e1b2bdeb 100644 --- a/pkg/gui/diffing.go +++ b/pkg/gui/diffing.go @@ -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() diff --git a/pkg/gui/filetree/commit_file_tree_view_model.go b/pkg/gui/filetree/commit_file_tree_view_model.go index 72960c702..a022bc25e 100644 --- a/pkg/gui/filetree/commit_file_tree_view_model.go +++ b/pkg/gui/filetree/commit_file_tree_view_model.go @@ -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 { diff --git a/pkg/gui/modes/diffing/diffing.go b/pkg/gui/modes/diffing/diffing.go index b27662b72..aa13bd1c1 100644 --- a/pkg/gui/modes/diffing/diffing.go +++ b/pkg/gui/modes/diffing/diffing.go @@ -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() { diff --git a/pkg/gui/patch_building_panel.go b/pkg/gui/patch_building_panel.go index f1aea7b55..87f4ccd92 100644 --- a/pkg/gui/patch_building_panel.go +++ b/pkg/gui/patch_building_panel.go @@ -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 diff --git a/pkg/gui/presentation/files.go b/pkg/gui/presentation/files.go index 13b2a64b0..3f4e34f14 100644 --- a/pkg/gui/presentation/files.go +++ b/pkg/gui/presentation/files.go @@ -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 { diff --git a/pkg/gui/refresh.go b/pkg/gui/refresh.go index 244c0c050..d3aaf9454 100644 --- a/pkg/gui/refresh.go +++ b/pkg/gui/refresh.go @@ -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() } diff --git a/pkg/gui/types/ref.go b/pkg/gui/types/ref.go new file mode 100644 index 000000000..49a15b275 --- /dev/null +++ b/pkg/gui/types/ref.go @@ -0,0 +1,7 @@ +package types + +type Ref interface { + RefName() string + ParentRefName() string + Description() string +}