From 59f5f5c1af4ab52f91d8bc6538fb460a21e880a8 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Fri, 21 Aug 2020 20:50:54 +1000 Subject: [PATCH] refactor --- pkg/commands/patch/patch_manager.go | 3 ++- pkg/gui/commit_files_panel.go | 24 ++++++++++++++++++++---- pkg/gui/commits_panel.go | 2 +- pkg/gui/gui.go | 2 +- pkg/gui/reflog_panel.go | 2 +- pkg/gui/stash_panel.go | 2 +- 6 files changed, 26 insertions(+), 9 deletions(-) diff --git a/pkg/commands/patch/patch_manager.go b/pkg/commands/patch/patch_manager.go index 8da6fb3a2..2af269fb3 100644 --- a/pkg/commands/patch/patch_manager.go +++ b/pkg/commands/patch/patch_manager.go @@ -46,8 +46,9 @@ func NewPatchManager(log *logrus.Entry, applyPatch applyPatchFunc) *PatchManager } // NewPatchManager returns a new PatchManager -func (p *PatchManager) Start(parent string, diffMap map[string]string) { +func (p *PatchManager) Start(parent string, diffMap map[string]string, canRebase bool) { p.Parent = parent + p.CanRebase = canRebase p.fileInfoMap = map[string]*fileInfo{} for filename, diff := range diffMap { p.fileInfoMap[filename] = &fileInfo{ diff --git a/pkg/gui/commit_files_panel.go b/pkg/gui/commit_files_panel.go index 2cdf0c02f..556c1088c 100644 --- a/pkg/gui/commit_files_panel.go +++ b/pkg/gui/commit_files_panel.go @@ -6,6 +6,20 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands" ) +const ( + // these are the possible ref types for refs that you can view files of. + // for local commits, we're allowed to build a patch and do things involving rebasing + // with that patch + REF_TYPE_LOCAL_COMMIT = iota + + // for other kinds of commits like reflog commits, we can't do anything rebasey + REF_TYPE_OTHER_COMMIT + + // for stash entries we can't do anything rebasey, and the command for + // obtaining the files is slightly different + REF_TYPE_STASH +) + func (gui *Gui) getSelectedCommitFile() *commands.CommitFile { selectedLine := gui.State.Panels.CommitFiles.SelectedLineIdx if selectedLine == -1 { @@ -80,7 +94,8 @@ func (gui *Gui) refreshCommitFilesView() error { return err } - files, err := gui.GitCommand.GetFilesInRef(gui.State.Panels.CommitFiles.refName, gui.State.Panels.CommitFiles.isStash, gui.GitCommand.PatchManager) + isStash := gui.State.Panels.CommitFiles.refType == REF_TYPE_STASH + files, err := gui.GitCommand.GetFilesInRef(gui.State.Panels.CommitFiles.refName, isStash, gui.GitCommand.PatchManager) if err != nil { return gui.surfaceError(err) } @@ -159,7 +174,8 @@ func (gui *Gui) startPatchManager() error { diffMap[commitFile.Name] = commitText } - gui.GitCommand.PatchManager.Start(gui.State.Panels.CommitFiles.refName, diffMap) + canRebase := gui.State.Panels.CommitFiles.refType == REF_TYPE_LOCAL_COMMIT + gui.GitCommand.PatchManager.Start(gui.State.Panels.CommitFiles.refName, diffMap, canRebase) return nil } @@ -210,14 +226,14 @@ func (gui *Gui) enterCommitFile(selectedLineIdx int) error { return enterTheFile(selectedLineIdx) } -func (gui *Gui) switchToCommitFilesContext(refName string, isStash bool, context Context, windowName string) error { +func (gui *Gui) switchToCommitFilesContext(refName string, refType int, context Context, windowName string) error { // sometimes the commitFiles view is already shown in another window, so we need to ensure that window // no longer considers the commitFiles view as its main view. gui.resetWindowForView("commitFiles") gui.State.Panels.CommitFiles.SelectedLineIdx = 0 gui.State.Panels.CommitFiles.refName = refName - gui.State.Panels.CommitFiles.isStash = isStash + gui.State.Panels.CommitFiles.refType = refType gui.Contexts.CommitFiles.Context.SetParentContext(context) gui.Contexts.CommitFiles.Context.SetWindowName(windowName) diff --git a/pkg/gui/commits_panel.go b/pkg/gui/commits_panel.go index 697da9ee7..427f62b71 100644 --- a/pkg/gui/commits_panel.go +++ b/pkg/gui/commits_panel.go @@ -505,7 +505,7 @@ func (gui *Gui) handleViewCommitFiles() error { return nil } - return gui.switchToCommitFilesContext(commit.Sha, false, gui.Contexts.BranchCommits.Context, "commits") + return gui.switchToCommitFilesContext(commit.Sha, REF_TYPE_LOCAL_COMMIT, gui.Contexts.BranchCommits.Context, "commits") } func (gui *Gui) hasCommit(commits []*commands.Commit, target string) (int, bool) { diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index aa7a72d0a..1b6446909 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -188,7 +188,7 @@ type commitFilesPanelState struct { // this is the SHA of the commit or the stash index of the stash. // Not sure if ref is actually the right word here refName string - isStash bool // true if we're dealing with a stash entry rather than a commit + refType int // eg REF_TYPE_LOCAL_COMMIT } type panelStates struct { diff --git a/pkg/gui/reflog_panel.go b/pkg/gui/reflog_panel.go index 7c6bcb766..c30ae96e9 100644 --- a/pkg/gui/reflog_panel.go +++ b/pkg/gui/reflog_panel.go @@ -119,5 +119,5 @@ func (gui *Gui) handleViewReflogCommitFiles() error { return nil } - return gui.switchToCommitFilesContext(commit.Sha, false, gui.Contexts.ReflogCommits.Context, "commits") + return gui.switchToCommitFilesContext(commit.Sha, REF_TYPE_OTHER_COMMIT, gui.Contexts.ReflogCommits.Context, "commits") } diff --git a/pkg/gui/stash_panel.go b/pkg/gui/stash_panel.go index 97f04aa0a..37a82890e 100644 --- a/pkg/gui/stash_panel.go +++ b/pkg/gui/stash_panel.go @@ -135,5 +135,5 @@ func (gui *Gui) handleViewStashFiles() error { return nil } - return gui.switchToCommitFilesContext(stashEntry.RefName(), true, gui.Contexts.Stash.Context, "stash") + return gui.switchToCommitFilesContext(stashEntry.RefName(), REF_TYPE_STASH, gui.Contexts.Stash.Context, "stash") }