1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-11-30 09:16:47 +02:00
lazygit/pkg/gui/files_panel.go

460 lines
12 KiB
Go
Raw Normal View History

2018-08-14 11:05:26 +02:00
package gui
import (
// "io"
// "io/ioutil"
// "strings"
"fmt"
2018-08-14 11:05:26 +02:00
"strings"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/utils"
2018-08-14 11:05:26 +02:00
)
func (gui *Gui) stagedFiles() []*commands.File {
2018-08-14 11:05:26 +02:00
files := gui.State.Files
result := make([]*commands.File, 0)
2018-08-14 11:05:26 +02:00
for _, file := range files {
if file.HasStagedChanges {
result = append(result, file)
}
}
return result
}
func (gui *Gui) trackedFiles() []*commands.File {
2018-08-14 11:05:26 +02:00
files := gui.State.Files
result := make([]*commands.File, 0)
2018-08-14 11:05:26 +02:00
for _, file := range files {
if file.Tracked {
result = append(result, file)
}
}
return result
}
func (gui *Gui) stageSelectedFile(g *gocui.Gui) error {
file, err := gui.getSelectedFile(g)
if err != nil {
return err
}
return gui.GitCommand.StageFile(file.Name)
}
func (gui *Gui) handleFilePress(g *gocui.Gui, v *gocui.View) error {
file, err := gui.getSelectedFile(g)
if err != nil {
if err == gui.Errors.ErrNoFiles {
2018-08-14 11:05:26 +02:00
return nil
}
return err
}
if file.HasMergeConflicts {
return gui.handleSwitchToMerge(g, v)
}
if file.HasUnstagedChanges {
gui.GitCommand.StageFile(file.Name)
} else {
gui.GitCommand.UnStageFile(file.Name, file.Tracked)
}
if err := gui.refreshFiles(g); err != nil {
return err
}
return gui.handleFileSelect(g, v)
}
func (gui *Gui) allFilesStaged() bool {
for _, file := range gui.State.Files {
if file.HasUnstagedChanges {
return false
}
}
return true
}
func (gui *Gui) handleStageAll(g *gocui.Gui, v *gocui.View) error {
var err error
if gui.allFilesStaged() {
err = gui.GitCommand.UnstageAll()
} else {
err = gui.GitCommand.StageAll()
}
if err != nil {
_ = gui.createErrorPanel(g, err.Error())
}
if err := gui.refreshFiles(g); err != nil {
return err
}
return gui.handleFileSelect(g, v)
}
2018-08-14 11:05:26 +02:00
func (gui *Gui) handleAddPatch(g *gocui.Gui, v *gocui.View) error {
file, err := gui.getSelectedFile(g)
if err != nil {
if err == gui.Errors.ErrNoFiles {
2018-08-14 11:05:26 +02:00
return nil
}
return err
}
if !file.HasUnstagedChanges {
return gui.createErrorPanel(g, gui.Tr.SLocalize("FileHasNoUnstagedChanges"))
2018-08-14 11:05:26 +02:00
}
if !file.Tracked {
return gui.createErrorPanel(g, gui.Tr.SLocalize("CannotGitAdd"))
2018-08-14 11:05:26 +02:00
}
2018-08-21 22:33:25 +02:00
gui.SubProcess = gui.GitCommand.AddPatch(file.Name)
return gui.Errors.ErrSubProcess
2018-08-14 11:05:26 +02:00
}
func (gui *Gui) getSelectedFile(g *gocui.Gui) (*commands.File, error) {
2018-08-14 11:05:26 +02:00
if len(gui.State.Files) == 0 {
return &commands.File{}, gui.Errors.ErrNoFiles
2018-08-14 11:05:26 +02:00
}
filesView, err := g.View("files")
if err != nil {
panic(err)
}
lineNumber := gui.getItemPosition(filesView)
return gui.State.Files[lineNumber], nil
}
func (gui *Gui) handleFileRemove(g *gocui.Gui, v *gocui.View) error {
file, err := gui.getSelectedFile(g)
if err != nil {
if err == gui.Errors.ErrNoFiles {
2018-08-14 11:05:26 +02:00
return nil
}
return err
}
var deleteVerb string
if file.Tracked {
deleteVerb = gui.Tr.SLocalize("checkout")
2018-08-14 11:05:26 +02:00
} else {
deleteVerb = gui.Tr.SLocalize("delete")
2018-08-14 21:06:50 +02:00
}
message := gui.Tr.TemplateLocalize(
"SureTo",
Teml{
2018-08-14 21:06:50 +02:00
"deleteVerb": deleteVerb,
"fileName": file.Name,
},
)
return gui.createConfirmationPanel(g, v, strings.Title(deleteVerb)+" file", message, func(g *gocui.Gui, v *gocui.View) error {
2018-08-14 11:05:26 +02:00
if err := gui.GitCommand.RemoveFile(file); err != nil {
2018-08-18 12:14:44 +02:00
return err
2018-08-14 11:05:26 +02:00
}
return gui.refreshFiles(g)
}, nil)
}
func (gui *Gui) handleIgnoreFile(g *gocui.Gui, v *gocui.View) error {
file, err := gui.getSelectedFile(g)
if err != nil {
return gui.createErrorPanel(g, err.Error())
}
if file.Tracked {
return gui.createErrorPanel(g, gui.Tr.SLocalize("CantIgnoreTrackFiles"))
2018-08-14 11:05:26 +02:00
}
2018-08-19 12:41:04 +02:00
if err := gui.GitCommand.Ignore(file.Name); err != nil {
return gui.createErrorPanel(g, err.Error())
}
2018-08-14 11:05:26 +02:00
return gui.refreshFiles(g)
}
func (gui *Gui) renderfilesOptions(g *gocui.Gui, file *commands.File) error {
return gui.renderGlobalOptions(g)
2018-08-14 11:05:26 +02:00
}
func (gui *Gui) handleFileSelect(g *gocui.Gui, v *gocui.View) error {
file, err := gui.getSelectedFile(g)
if err != nil {
if err != gui.Errors.ErrNoFiles {
2018-08-14 11:05:26 +02:00
return err
}
gui.renderString(g, "main", gui.Tr.SLocalize("NoChangedFiles"))
2018-08-14 11:05:26 +02:00
return gui.renderfilesOptions(g, nil)
}
2018-09-17 13:32:19 +02:00
if err := gui.renderfilesOptions(g, file); err != nil {
return err
}
2018-08-14 11:05:26 +02:00
var content string
if file.HasMergeConflicts {
return gui.refreshMergePanel(g)
}
content = gui.GitCommand.Diff(file)
return gui.renderString(g, "main", content)
}
func (gui *Gui) handleCommitPress(g *gocui.Gui, filesView *gocui.View) error {
if len(gui.stagedFiles()) == 0 && !gui.State.HasMergeConflicts {
return gui.createErrorPanel(g, gui.Tr.SLocalize("NoStagedFilesToCommit"))
2018-08-14 11:05:26 +02:00
}
commitMessageView := gui.getCommitMessageView(g)
2018-09-12 15:20:35 +02:00
g.Update(func(g *gocui.Gui) error {
g.SetViewOnTop("commitMessage")
gui.switchFocus(g, filesView, commitMessageView)
gui.RenderCommitLength()
return nil
})
return nil
}
func (gui *Gui) handleAmendCommitPress(g *gocui.Gui, filesView *gocui.View) error {
if len(gui.stagedFiles()) == 0 && !gui.State.HasMergeConflicts {
return gui.createErrorPanel(g, gui.Tr.SLocalize("NoStagedFilesToCommit"))
}
title := strings.Title(gui.Tr.SLocalize("AmendLastCommit"))
question := gui.Tr.SLocalize("SureToAmend")
if len(gui.State.Commits) == 0 {
return gui.createErrorPanel(g, gui.Tr.SLocalize("NoCommitToAmend"))
}
return gui.createConfirmationPanel(g, filesView, title, question, func(g *gocui.Gui, v *gocui.View) error {
lastCommitMsg := gui.State.Commits[0].Name
_, err := gui.GitCommand.Commit(lastCommitMsg, true)
if err != nil {
return gui.createErrorPanel(g, err.Error())
}
2018-09-12 15:20:35 +02:00
return gui.refreshSidePanels(g)
}, nil)
2018-08-14 11:05:26 +02:00
}
// handleCommitEditorPress - handle when the user wants to commit changes via
// their editor rather than via the popup panel
func (gui *Gui) handleCommitEditorPress(g *gocui.Gui, filesView *gocui.View) error {
if len(gui.stagedFiles()) == 0 && !gui.State.HasMergeConflicts {
return gui.createErrorPanel(g, gui.Tr.SLocalize("NoStagedFilesToCommit"))
2018-08-14 11:05:26 +02:00
}
gui.PrepareSubProcess(g, "git", "commit")
return nil
}
// PrepareSubProcess - prepare a subprocess for execution and tell the gui to switch to it
2018-08-21 22:33:25 +02:00
func (gui *Gui) PrepareSubProcess(g *gocui.Gui, commands ...string) {
gui.SubProcess = gui.GitCommand.PrepareCommitSubProcess()
2018-08-14 11:05:26 +02:00
g.Update(func(g *gocui.Gui) error {
return gui.Errors.ErrSubProcess
2018-08-14 11:05:26 +02:00
})
}
func (gui *Gui) editFile(filename string) error {
sub, err := gui.OSCommand.EditFile(filename)
2018-08-14 11:05:26 +02:00
if err != nil {
return gui.createErrorPanel(gui.g, err.Error())
2018-08-14 11:05:26 +02:00
}
if sub != nil {
gui.SubProcess = sub
return gui.Errors.ErrSubProcess
2018-08-14 11:05:26 +02:00
}
return nil
}
func (gui *Gui) handleFileEdit(g *gocui.Gui, v *gocui.View) error {
file, err := gui.getSelectedFile(g)
if err != nil {
return err
}
return gui.editFile(file.Name)
2018-08-14 11:05:26 +02:00
}
func (gui *Gui) handleFileOpen(g *gocui.Gui, v *gocui.View) error {
file, err := gui.getSelectedFile(g)
if err != nil {
return err
}
2018-08-23 21:05:09 +02:00
return gui.openFile(file.Name)
2018-08-14 11:05:26 +02:00
}
func (gui *Gui) handleRefreshFiles(g *gocui.Gui, v *gocui.View) error {
return gui.refreshFiles(g)
}
func (gui *Gui) refreshStateFiles() {
// get files to stage
files := gui.GitCommand.GetStatusFiles()
gui.State.Files = gui.GitCommand.MergeStatusFiles(gui.State.Files, files)
gui.updateHasMergeConflictStatus()
}
func (gui *Gui) updateHasMergeConflictStatus() error {
merging, err := gui.GitCommand.IsInMergeState()
if err != nil {
return err
}
gui.State.HasMergeConflicts = merging
return nil
}
func (gui *Gui) catSelectedFile(g *gocui.Gui) (string, error) {
item, err := gui.getSelectedFile(g)
if err != nil {
if err != gui.Errors.ErrNoFiles {
2018-08-14 11:05:26 +02:00
return "", err
}
return "", gui.renderString(g, "main", gui.Tr.SLocalize("NoFilesDisplay"))
2018-08-14 11:05:26 +02:00
}
2018-08-28 11:12:35 +02:00
if item.Type != "file" {
return "", gui.renderString(g, "main", gui.Tr.SLocalize("NotAFile"))
}
2018-08-14 11:05:26 +02:00
cat, err := gui.GitCommand.CatFile(item.Name)
if err != nil {
2018-08-28 11:12:35 +02:00
gui.Log.Error(err)
return "", gui.renderString(g, "main", err.Error())
2018-08-14 11:05:26 +02:00
}
return cat, nil
}
func (gui *Gui) refreshFiles(g *gocui.Gui) error {
filesView, err := g.View("files")
if err != nil {
return err
}
gui.refreshStateFiles()
2018-08-14 11:05:26 +02:00
filesView.Clear()
list, err := utils.RenderList(gui.State.Files)
if err != nil {
return err
2018-08-14 11:05:26 +02:00
}
fmt.Fprint(filesView, list)
2018-08-14 11:05:26 +02:00
gui.correctCursor(filesView)
if filesView == g.CurrentView() {
gui.handleFileSelect(g, filesView)
}
return nil
}
func (gui *Gui) pullFiles(g *gocui.Gui, v *gocui.View) error {
if err := gui.createMessagePanel(gui.g, v, "", gui.Tr.SLocalize("PullWait")); err != nil {
return err
}
2018-08-14 11:05:26 +02:00
go func() {
unamePassOpend := false
err := gui.GitCommand.Pull(func(passOrUname string) string {
unamePassOpend = true
return gui.waitForPassUname(g, v, passOrUname)
})
if err != nil {
errMessage := err.Error()
if errMessage == "exit status 128" {
errMessage = gui.Tr.SLocalize("PassUnameWrong")
}
_ = gui.createErrorPanel(g, errMessage)
}
if unamePassOpend {
_, _ = g.SetViewOnBottom("pushPassUname")
_ = g.DeleteView("pushPassUname")
}
if err == nil {
_ = gui.closeConfirmationPrompt(g)
_ = gui.refreshCommits(g)
_ = gui.refreshStatus(g)
2018-08-14 11:05:26 +02:00
}
gui.refreshFiles(g)
}()
return nil
}
func (gui *Gui) pushWithForceFlag(g *gocui.Gui, v *gocui.View, force bool) error {
if err := gui.createMessagePanel(g, v, "", gui.Tr.SLocalize("PushWait")); err != nil {
return err
}
2018-08-14 11:05:26 +02:00
go func() {
unamePassOpend := false
2018-08-14 11:05:26 +02:00
branchName := gui.State.Branches[0].Name
err := gui.GitCommand.Push(branchName, force, func(passOrUname string) string {
unamePassOpend = true
return gui.waitForPassUname(g, v, passOrUname)
})
if err != nil {
2018-10-20 17:37:55 +02:00
errMessage := err.Error()
if errMessage == "exit status 128" {
errMessage = gui.Tr.SLocalize("PassUnameWrong")
}
_ = gui.createErrorPanel(g, errMessage)
}
if unamePassOpend {
_, _ = g.SetViewOnBottom("pushPassUname")
_ = g.DeleteView("pushPassUname")
}
if err == nil {
_ = gui.closeConfirmationPrompt(g)
_ = gui.refreshCommits(g)
_ = gui.refreshStatus(g)
2018-08-14 11:05:26 +02:00
}
}()
return nil
}
func (gui *Gui) pushFiles(g *gocui.Gui, v *gocui.View) error {
// if we have pullables we'll ask if the user wants to force push
_, pullables := gui.GitCommand.UpstreamDifferenceCount()
if pullables == "?" || pullables == "0" {
2018-10-20 17:37:55 +02:00
return gui.pushWithForceFlag(g, v, false)
}
err := gui.createConfirmationPanel(g, nil, gui.Tr.SLocalize("ForcePush"), gui.Tr.SLocalize("ForcePushPrompt"), func(g *gocui.Gui, v *gocui.View) error {
2018-10-20 17:37:55 +02:00
return gui.pushWithForceFlag(g, v, true)
}, nil)
return err
}
2018-08-14 11:05:26 +02:00
func (gui *Gui) handleSwitchToMerge(g *gocui.Gui, v *gocui.View) error {
mergeView, err := g.View("main")
if err != nil {
return err
}
file, err := gui.getSelectedFile(g)
if err != nil {
if err != gui.Errors.ErrNoFiles {
2018-08-14 11:05:26 +02:00
return err
}
return nil
}
if !file.HasMergeConflicts {
return gui.createErrorPanel(g, gui.Tr.SLocalize("FileNoMergeCons"))
2018-08-14 11:05:26 +02:00
}
gui.switchFocus(g, v, mergeView)
return gui.refreshMergePanel(g)
}
func (gui *Gui) handleAbortMerge(g *gocui.Gui, v *gocui.View) error {
if err := gui.GitCommand.AbortMerge(); err != nil {
return gui.createErrorPanel(g, err.Error())
}
gui.createMessagePanel(g, v, "", gui.Tr.SLocalize("MergeAborted"))
2018-08-14 11:05:26 +02:00
gui.refreshStatus(g)
return gui.refreshFiles(g)
}
func (gui *Gui) handleResetHard(g *gocui.Gui, v *gocui.View) error {
return gui.createConfirmationPanel(g, v, gui.Tr.SLocalize("ClearFilePanel"), gui.Tr.SLocalize("SureResetHardHead"), func(g *gocui.Gui, v *gocui.View) error {
2018-08-14 11:05:26 +02:00
if err := gui.GitCommand.ResetHard(); err != nil {
gui.createErrorPanel(g, err.Error())
}
return gui.refreshFiles(g)
}, nil)
}
2018-08-23 21:05:09 +02:00
func (gui *Gui) openFile(filename string) error {
if err := gui.OSCommand.OpenFile(filename); err != nil {
return gui.createErrorPanel(gui.g, err.Error())
}
return nil
}