mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-15 00:15:32 +02:00
don't let patch manager ever be nil
This commit is contained in:
@ -109,7 +109,7 @@ func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer,
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &GitCommand{
|
gitCommand := &GitCommand{
|
||||||
Log: log,
|
Log: log,
|
||||||
OSCommand: osCommand,
|
OSCommand: osCommand,
|
||||||
Tr: tr,
|
Tr: tr,
|
||||||
@ -120,7 +120,11 @@ func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer,
|
|||||||
getLocalGitConfig: gitconfig.Local,
|
getLocalGitConfig: gitconfig.Local,
|
||||||
removeFile: os.RemoveAll,
|
removeFile: os.RemoveAll,
|
||||||
DotGitDir: dotGitDir,
|
DotGitDir: dotGitDir,
|
||||||
}, nil
|
}
|
||||||
|
|
||||||
|
gitCommand.PatchManager = NewPatchManager(log, gitCommand.ApplyPatch)
|
||||||
|
|
||||||
|
return gitCommand, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func findDotGitDir(stat func(string) (os.FileInfo, error), readFile func(filename string) ([]byte, error)) (string, error) {
|
func findDotGitDir(stat func(string) (os.FileInfo, error), readFile func(filename string) ([]byte, error)) (string, error) {
|
||||||
|
@ -25,20 +25,22 @@ type PatchManager struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewPatchManager returns a new PatchModifier
|
// NewPatchManager returns a new PatchModifier
|
||||||
func NewPatchManager(log *logrus.Entry, applyPatch applyPatchFunc, commitSha string, diffMap map[string]string) *PatchManager {
|
func NewPatchManager(log *logrus.Entry, applyPatch applyPatchFunc) *PatchManager {
|
||||||
infoMap := map[string]*fileInfo{}
|
return &PatchManager{
|
||||||
for filename, diff := range diffMap {
|
Log: log,
|
||||||
infoMap[filename] = &fileInfo{
|
ApplyPatch: applyPatch,
|
||||||
mode: UNSELECTED,
|
|
||||||
diff: diff,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &PatchManager{
|
// NewPatchManager returns a new PatchModifier
|
||||||
Log: log,
|
func (p *PatchManager) Start(commitSha string, diffMap map[string]string) {
|
||||||
fileInfoMap: infoMap,
|
p.CommitSha = commitSha
|
||||||
CommitSha: commitSha,
|
p.fileInfoMap = map[string]*fileInfo{}
|
||||||
ApplyPatch: applyPatch,
|
for filename, diff := range diffMap {
|
||||||
|
p.fileInfoMap[filename] = &fileInfo{
|
||||||
|
mode: UNSELECTED,
|
||||||
|
diff: diff,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -200,3 +202,13 @@ func (p *PatchManager) ApplyPatches(reverse bool) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// clears the patch
|
||||||
|
func (p *PatchManager) Reset() {
|
||||||
|
p.CommitSha = ""
|
||||||
|
p.fileInfoMap = map[string]*fileInfo{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PatchManager) IsEmpty() bool {
|
||||||
|
return p != nil && p.CommitSha == "" || len(p.fileInfoMap) == 0
|
||||||
|
}
|
||||||
|
@ -22,7 +22,7 @@ func (c *GitCommand) DeletePatchesFromCommit(commits []*Commit, commitIndex int,
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.onSuccessfulContinue = func() error {
|
c.onSuccessfulContinue = func() error {
|
||||||
c.PatchManager = nil
|
c.PatchManager.Reset()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,7 +50,7 @@ func (c *GitCommand) MovePatchToSelectedCommit(commits []*Commit, sourceCommitId
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.onSuccessfulContinue = func() error {
|
c.onSuccessfulContinue = func() error {
|
||||||
c.PatchManager = nil
|
c.PatchManager.Reset()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,7 +121,7 @@ func (c *GitCommand) MovePatchToSelectedCommit(commits []*Commit, sourceCommitId
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.onSuccessfulContinue = func() error {
|
c.onSuccessfulContinue = func() error {
|
||||||
c.PatchManager = nil
|
c.PatchManager.Reset()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,7 +161,7 @@ func (c *GitCommand) PullPatchIntoIndex(commits []*Commit, commitIdx int, p *Pat
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
c.PatchManager = nil
|
c.PatchManager.Reset()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,8 +136,8 @@ func (gui *Gui) handleToggleFileForPatch(g *gocui.Gui, v *gocui.View) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
toggleTheFile := func() error {
|
toggleTheFile := func() error {
|
||||||
if gui.GitCommand.PatchManager == nil {
|
if gui.GitCommand.PatchManager.IsEmpty() {
|
||||||
if err := gui.createPatchManager(); err != nil {
|
if err := gui.startPatchManager(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -147,9 +147,9 @@ func (gui *Gui) handleToggleFileForPatch(g *gocui.Gui, v *gocui.View) error {
|
|||||||
return gui.refreshCommitFilesView()
|
return gui.refreshCommitFilesView()
|
||||||
}
|
}
|
||||||
|
|
||||||
if gui.GitCommand.PatchManager != nil && gui.GitCommand.PatchManager.CommitSha != commitFile.Sha {
|
if !gui.GitCommand.PatchManager.IsEmpty() && gui.GitCommand.PatchManager.CommitSha != commitFile.Sha {
|
||||||
return gui.createConfirmationPanel(g, v, true, gui.Tr.SLocalize("DiscardPatch"), gui.Tr.SLocalize("DiscardPatchConfirm"), func(g *gocui.Gui, v *gocui.View) error {
|
return gui.createConfirmationPanel(g, v, true, gui.Tr.SLocalize("DiscardPatch"), gui.Tr.SLocalize("DiscardPatchConfirm"), func(g *gocui.Gui, v *gocui.View) error {
|
||||||
gui.GitCommand.PatchManager = nil
|
gui.GitCommand.PatchManager.Reset()
|
||||||
return toggleTheFile()
|
return toggleTheFile()
|
||||||
}, nil)
|
}, nil)
|
||||||
}
|
}
|
||||||
@ -157,7 +157,7 @@ func (gui *Gui) handleToggleFileForPatch(g *gocui.Gui, v *gocui.View) error {
|
|||||||
return toggleTheFile()
|
return toggleTheFile()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) createPatchManager() error {
|
func (gui *Gui) startPatchManager() error {
|
||||||
diffMap := map[string]string{}
|
diffMap := map[string]string{}
|
||||||
for _, commitFile := range gui.State.CommitFiles {
|
for _, commitFile := range gui.State.CommitFiles {
|
||||||
commitText, err := gui.GitCommand.ShowCommitFile(commitFile.Sha, commitFile.Name, true)
|
commitText, err := gui.GitCommand.ShowCommitFile(commitFile.Sha, commitFile.Name, true)
|
||||||
@ -172,7 +172,7 @@ func (gui *Gui) createPatchManager() error {
|
|||||||
return errors.New("No commit selected")
|
return errors.New("No commit selected")
|
||||||
}
|
}
|
||||||
|
|
||||||
gui.GitCommand.PatchManager = commands.NewPatchManager(gui.Log, gui.GitCommand.ApplyPatch, commit.Sha, diffMap)
|
gui.GitCommand.PatchManager.Start(commit.Sha, diffMap)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,8 +187,8 @@ func (gui *Gui) handleEnterCommitFile(g *gocui.Gui, v *gocui.View) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
enterTheFile := func() error {
|
enterTheFile := func() error {
|
||||||
if gui.GitCommand.PatchManager == nil {
|
if gui.GitCommand.PatchManager.IsEmpty() {
|
||||||
if err := gui.createPatchManager(); err != nil {
|
if err := gui.startPatchManager(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -202,9 +202,9 @@ func (gui *Gui) handleEnterCommitFile(g *gocui.Gui, v *gocui.View) error {
|
|||||||
return gui.refreshPatchBuildingPanel()
|
return gui.refreshPatchBuildingPanel()
|
||||||
}
|
}
|
||||||
|
|
||||||
if gui.GitCommand.PatchManager != nil && gui.GitCommand.PatchManager.CommitSha != commitFile.Sha {
|
if !gui.GitCommand.PatchManager.IsEmpty() && gui.GitCommand.PatchManager.CommitSha != commitFile.Sha {
|
||||||
return gui.createConfirmationPanel(g, v, false, gui.Tr.SLocalize("DiscardPatch"), gui.Tr.SLocalize("DiscardPatchConfirm"), func(g *gocui.Gui, v *gocui.View) error {
|
return gui.createConfirmationPanel(g, v, false, gui.Tr.SLocalize("DiscardPatch"), gui.Tr.SLocalize("DiscardPatchConfirm"), func(g *gocui.Gui, v *gocui.View) error {
|
||||||
gui.GitCommand.PatchManager = nil
|
gui.GitCommand.PatchManager.Reset()
|
||||||
return enterTheFile()
|
return enterTheFile()
|
||||||
}, nil)
|
}, nil)
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (gui *Gui) refreshPatchBuildingPanel() error {
|
func (gui *Gui) refreshPatchBuildingPanel() error {
|
||||||
if gui.GitCommand.PatchManager == nil {
|
if gui.GitCommand.PatchManager.IsEmpty() {
|
||||||
return gui.handleEscapePatchBuildingPanel(gui.g, nil)
|
return gui.handleEscapePatchBuildingPanel(gui.g, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,7 +91,7 @@ func (gui *Gui) handleEscapePatchBuildingPanel(g *gocui.Gui, v *gocui.View) erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) refreshSecondaryPatchPanel() error {
|
func (gui *Gui) refreshSecondaryPatchPanel() error {
|
||||||
if gui.GitCommand.PatchManager != nil {
|
if !gui.GitCommand.PatchManager.IsEmpty() {
|
||||||
gui.State.SplitMainPanel = true
|
gui.State.SplitMainPanel = true
|
||||||
secondaryView := gui.getSecondaryView()
|
secondaryView := gui.getSecondaryView()
|
||||||
secondaryView.Highlight = true
|
secondaryView.Highlight = true
|
||||||
|
@ -17,15 +17,14 @@ func (o *patchMenuOption) GetDisplayStrings(isFocused bool) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) handleCreatePatchOptionsMenu(g *gocui.Gui, v *gocui.View) error {
|
func (gui *Gui) handleCreatePatchOptionsMenu(g *gocui.Gui, v *gocui.View) error {
|
||||||
m := gui.GitCommand.PatchManager
|
if gui.GitCommand.PatchManager.IsEmpty() {
|
||||||
if m == nil {
|
|
||||||
return gui.createErrorPanel(gui.g, gui.Tr.SLocalize("NoPatchError"))
|
return gui.createErrorPanel(gui.g, gui.Tr.SLocalize("NoPatchError"))
|
||||||
}
|
}
|
||||||
|
|
||||||
options := []*patchMenuOption{
|
options := []*patchMenuOption{
|
||||||
{displayName: fmt.Sprintf("remove patch from original commit (%s)", gui.GitCommand.PatchManager.CommitSha), function: gui.handleDeletePatchFromCommit},
|
{displayName: fmt.Sprintf("remove patch from original commit (%s)", gui.GitCommand.PatchManager.CommitSha), function: gui.handleDeletePatchFromCommit},
|
||||||
{displayName: "pull patch out into index", function: gui.handlePullPatchIntoWorkingTree},
|
{displayName: "pull patch out into index", function: gui.handlePullPatchIntoWorkingTree},
|
||||||
{displayName: "reset patch", function: gui.handleClearPatch},
|
{displayName: "reset patch", function: gui.handleResetPatch},
|
||||||
}
|
}
|
||||||
|
|
||||||
selectedCommit := gui.getSelectedCommit(gui.g)
|
selectedCommit := gui.getSelectedCommit(gui.g)
|
||||||
@ -122,7 +121,7 @@ func (gui *Gui) handlePullPatchIntoWorkingTree() error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) handleClearPatch() error {
|
func (gui *Gui) handleResetPatch() error {
|
||||||
gui.GitCommand.PatchManager = nil
|
gui.GitCommand.PatchManager.Reset()
|
||||||
return gui.refreshCommitFilesView()
|
return gui.refreshCommitFilesView()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user