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

1012 lines
26 KiB
Go
Raw Normal View History

2018-08-14 11:05:26 +02:00
package gui
import (
"fmt"
"regexp"
2018-08-14 11:05:26 +02:00
"strings"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
2020-10-03 06:54:55 +02:00
"github.com/jesseduffield/lazygit/pkg/config"
2021-03-21 06:58:15 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/filetree"
"github.com/jesseduffield/lazygit/pkg/utils"
2018-08-14 11:05:26 +02:00
)
// list panel functions
func (gui *Gui) getSelectedFileNode() *filetree.FileNode {
2021-03-14 05:41:11 +02:00
selectedLine := gui.State.Panels.Files.SelectedLineIdx
if selectedLine == -1 {
return nil
}
return gui.State.FileManager.GetItemAtIndex(selectedLine)
2021-03-14 05:41:11 +02:00
}
2020-09-29 10:45:00 +02:00
func (gui *Gui) getSelectedFile() *models.File {
node := gui.getSelectedFileNode()
if node == nil {
return nil
}
return node.File
}
2021-03-15 14:00:20 +02:00
func (gui *Gui) getSelectedPath() string {
node := gui.getSelectedFileNode()
2021-03-15 14:00:20 +02:00
if node == nil {
return ""
}
return node.GetPath()
}
func (gui *Gui) filesRenderToMain() error {
node := gui.getSelectedFileNode()
2021-03-14 09:46:22 +02:00
if node == nil {
2020-08-23 01:46:28 +02:00
return gui.refreshMainViews(refreshMainOpts{
2020-08-18 14:02:35 +02:00
main: &viewUpdateOpts{
title: "",
2021-04-04 15:51:59 +02:00
task: NewRenderStringTask(gui.Tr.NoChangedFiles),
2020-08-18 14:02:35 +02:00
},
})
}
2021-03-14 09:46:22 +02:00
if node.File != nil && node.File.HasInlineMergeConflicts {
return gui.refreshMergePanelWithLock()
}
2021-12-07 12:59:36 +02:00
cmdObj := gui.GitCommand.WorktreeFileDiffCmdObj(node, false, !node.GetHasUnstagedChanges() && node.GetHasStagedChanges(), gui.State.IgnoreWhitespaceInDiffView)
2020-08-18 14:02:35 +02:00
refreshOpts := refreshMainOpts{main: &viewUpdateOpts{
2020-10-04 02:00:48 +02:00
title: gui.Tr.UnstagedChanges,
2021-12-07 12:59:36 +02:00
task: NewRunPtyTask(cmdObj.GetCmd()),
2020-08-18 14:02:35 +02:00
}}
2021-03-14 09:46:22 +02:00
if node.GetHasUnstagedChanges() {
if node.GetHasStagedChanges() {
2021-12-07 12:59:36 +02:00
cmdObj := gui.GitCommand.WorktreeFileDiffCmdObj(node, false, true, gui.State.IgnoreWhitespaceInDiffView)
2021-03-14 09:46:22 +02:00
refreshOpts.secondary = &viewUpdateOpts{
title: gui.Tr.StagedChanges,
2021-12-07 12:59:36 +02:00
task: NewRunPtyTask(cmdObj.GetCmd()),
2021-03-14 09:46:22 +02:00
}
2020-08-18 14:02:35 +02:00
}
2021-03-14 09:46:22 +02:00
} else {
2020-10-04 02:00:48 +02:00
refreshOpts.main.title = gui.Tr.StagedChanges
}
allow fast flicking through any list panel Up till now our approach to rendering things like file diffs, branch logs, and commit patches, has been to run a command on the command line, wait for it to complete, take its output as a string, and then write that string to the main view (or secondary view e.g. when showing both staged and unstaged changes of a file). This has caused various issues. For once, if you are flicking through a list of files and an untracked file is particularly large, not only will this require lazygit to load that whole file into memory (or more accurately it's equally large diff), it also will slow down the UI thread while loading that file, and if the user continued down the list, the original command might eventually resolve and replace whatever the diff is for the newly selected file. Following what we've done in lazydocker, I've added a tasks package for when you need something done but you want it to cancel as soon as something newer comes up. Given this typically involves running a command to display to a view, I've added a viewBufferManagerMap struct to the Gui struct which allows you to define these tasks on a per-view basis. viewBufferManagers can run files and directly write the output to their view, meaning we no longer need to use so much memory. In the tasks package there is a helper method called NewCmdTask which takes a command, an initial amount of lines to read, and then runs that command, reads that number of lines, and allows for a readLines channel to tell it to read more lines. We read more lines when we scroll or resize the window. There is an adapter for the tasks package in a file called tasks_adapter which wraps the functions from the tasks package in gui-specific stuff like clearing the main view before starting the next task that wants to write to the main view. I've removed some small features as part of this work, namely the little headers that were at the top of the main view for some situations. For example, we no longer show the upstream of a selected branch. I want to re-introduce this in the future, but I didn't want to make this tasks system too complicated, and in order to facilitate a header section in the main view we'd need to have a task that gets the upstream for the current branch, writes it to the header, then tells another task to write the branch log to the main view, but without clearing inbetween. So it would get messy. I'm thinking instead of having a separate 'header' view atop the main view to render that kind of thing (which can happen in another PR) I've also simplified the 'git show' to just call 'git show' and not do anything fancy when it comes to merge commits. I considered using this tasks approach whenever we write to a view. The only thing is that the renderString method currently resets the origin of a view and I don't want to lose that. So I've left some in there that I consider harmless, but we should probably be just using tasks now for all rendering, even if it's just strings we can instantly make.
2020-01-11 05:54:59 +02:00
2020-08-23 01:46:28 +02:00
return gui.refreshMainViews(refreshOpts)
}
2020-09-30 00:27:23 +02:00
func (gui *Gui) refreshFilesAndSubmodules() error {
gui.Mutexes.RefreshingFilesMutex.Lock()
gui.State.IsRefreshingFiles = true
defer func() {
gui.State.IsRefreshingFiles = false
gui.Mutexes.RefreshingFilesMutex.Unlock()
}()
2021-03-15 14:00:20 +02:00
selectedPath := gui.getSelectedPath()
2020-09-28 01:14:32 +02:00
if err := gui.refreshStateSubmoduleConfigs(); err != nil {
return err
}
2019-03-02 04:22:02 +02:00
if err := gui.refreshStateFiles(); err != nil {
return err
}
gui.g.Update(func(g *gocui.Gui) error {
2021-04-03 06:56:11 +02:00
if err := gui.postRefreshUpdate(gui.State.Contexts.Submodules); err != nil {
2020-09-30 00:27:23 +02:00
gui.Log.Error(err)
2020-08-19 10:41:57 +02:00
}
2021-04-04 17:10:23 +02:00
if ContextKey(gui.Views.Files.Context) == FILES_CONTEXT_KEY {
2020-09-30 00:27:23 +02:00
// doing this a little custom (as opposed to using gui.postRefreshUpdate) because we handle selecting the file explicitly below
2021-04-03 06:56:11 +02:00
if err := gui.State.Contexts.Files.HandleRender(); err != nil {
2020-09-30 00:27:23 +02:00
return err
}
}
2021-04-04 17:10:23 +02:00
if gui.currentContext().GetKey() == FILES_CONTEXT_KEY || (g.CurrentView() == gui.Views.Main && ContextKey(g.CurrentView().Context) == MAIN_MERGING_CONTEXT_KEY) {
2021-03-15 14:00:20 +02:00
newSelectedPath := gui.getSelectedPath()
alreadySelected := selectedPath != "" && newSelectedPath == selectedPath
if !alreadySelected {
gui.takeOverMergeConflictScrolling()
2020-09-30 00:27:23 +02:00
}
gui.Views.Files.FocusPoint(0, gui.State.Panels.Files.SelectedLineIdx)
return gui.filesRenderToMain()
}
2020-09-30 00:27:23 +02:00
return nil
})
return nil
}
// specific functions
2020-09-29 10:45:00 +02:00
func (gui *Gui) stagedFiles() []*models.File {
files := gui.State.FileManager.GetAllFiles()
2020-09-29 10:45:00 +02:00
result := make([]*models.File, 0)
2018-08-14 11:05:26 +02:00
for _, file := range files {
if file.HasStagedChanges {
result = append(result, file)
}
}
return result
}
2020-09-29 10:45:00 +02:00
func (gui *Gui) trackedFiles() []*models.File {
files := gui.State.FileManager.GetAllFiles()
2020-09-29 10:45:00 +02:00
result := make([]*models.File, 0, len(files))
2018-08-14 11:05:26 +02:00
for _, file := range files {
if file.Tracked {
result = append(result, file)
}
}
return result
}
2020-11-16 11:38:26 +02:00
func (gui *Gui) stageSelectedFile() error {
2020-08-16 09:45:12 +02:00
file := gui.getSelectedFile()
if file == nil {
return nil
2018-08-14 11:05:26 +02:00
}
2020-08-16 09:45:12 +02:00
2018-08-14 11:05:26 +02:00
return gui.GitCommand.StageFile(file.Name)
}
func (gui *Gui) handleEnterFile() error {
return gui.enterFile(OnFocusOpts{ClickedViewName: "", ClickedViewLineIdx: -1})
2019-11-10 07:20:35 +02:00
}
func (gui *Gui) enterFile(opts OnFocusOpts) error {
node := gui.getSelectedFileNode()
2021-03-14 11:18:06 +02:00
if node == nil {
return nil
}
2020-08-16 09:45:12 +02:00
2021-03-14 11:18:06 +02:00
if node.File == nil {
return gui.handleToggleDirCollapsed()
}
file := node.File
2020-09-30 00:27:23 +02:00
submoduleConfigs := gui.State.Submodules
if file.IsSubmodule(submoduleConfigs) {
submoduleConfig := file.SubmoduleConfig(submoduleConfigs)
2020-09-30 00:27:23 +02:00
return gui.enterSubmodule(submoduleConfig)
}
if file.HasInlineMergeConflicts {
2020-08-15 09:23:16 +02:00
return gui.handleSwitchToMerge()
}
if file.HasMergeConflicts {
2020-10-04 02:00:48 +02:00
return gui.createErrorPanel(gui.Tr.FileStagingRequirements)
}
2020-08-16 05:58:29 +02:00
return gui.pushContext(gui.State.Contexts.Staging, opts)
}
2020-08-15 09:23:16 +02:00
func (gui *Gui) handleFilePress() error {
node := gui.getSelectedFileNode()
2021-03-14 05:41:11 +02:00
if node == nil {
2020-08-16 09:45:12 +02:00
return nil
2018-08-14 11:05:26 +02:00
}
2021-03-14 05:41:11 +02:00
if node.IsLeaf() {
file := node.File
2018-08-14 11:05:26 +02:00
2021-03-14 05:41:11 +02:00
if file.HasInlineMergeConflicts {
return gui.handleSwitchToMerge()
}
if file.HasUnstagedChanges {
2021-04-11 11:35:42 +02:00
if err := gui.GitCommand.WithSpan(gui.Tr.Spans.StageFile).StageFile(file.Name); err != nil {
2021-03-14 05:41:11 +02:00
return gui.surfaceError(err)
}
} else {
2021-04-11 11:35:42 +02:00
if err := gui.GitCommand.WithSpan(gui.Tr.Spans.UnstageFile).UnStageFile(file.Names(), file.Tracked); err != nil {
2021-03-14 05:41:11 +02:00
return gui.surfaceError(err)
}
2020-08-16 09:45:12 +02:00
}
2018-08-14 11:05:26 +02:00
} else {
// if any files within have inline merge conflicts we can't stage or unstage,
// or it'll end up with those >>>>>> lines actually staged
if node.GetHasInlineMergeConflicts() {
return gui.createErrorPanel(gui.Tr.ErrStageDirWithInlineMergeConflicts)
}
2021-03-14 09:46:22 +02:00
if node.GetHasUnstagedChanges() {
2021-04-11 11:35:42 +02:00
if err := gui.GitCommand.WithSpan(gui.Tr.Spans.StageFile).StageFile(node.Path); err != nil {
2021-03-14 05:41:11 +02:00
return gui.surfaceError(err)
}
} else {
// pretty sure it doesn't matter that we're always passing true here
2021-04-11 11:35:42 +02:00
if err := gui.GitCommand.WithSpan(gui.Tr.Spans.UnstageFile).UnStageFile([]string{node.Path}, true); err != nil {
2021-03-14 05:41:11 +02:00
return gui.surfaceError(err)
}
2020-08-16 09:45:12 +02:00
}
2018-08-14 11:05:26 +02:00
}
if err := gui.refreshSidePanels(refreshOptions{scope: []RefreshableView{FILES}}); err != nil {
2018-08-14 11:05:26 +02:00
return err
}
return gui.State.Contexts.Files.HandleFocus()
2018-08-14 11:05:26 +02:00
}
func (gui *Gui) allFilesStaged() bool {
for _, file := range gui.State.FileManager.GetAllFiles() {
if file.HasUnstagedChanges {
return false
}
}
return true
}
func (gui *Gui) onFocusFile() error {
gui.takeOverMergeConflictScrolling()
return nil
}
func (gui *Gui) handleStageAll() error {
var err error
if gui.allFilesStaged() {
2021-04-11 11:35:42 +02:00
err = gui.GitCommand.WithSpan(gui.Tr.Spans.UnstageAllFiles).UnstageAll()
} else {
2021-04-11 11:35:42 +02:00
err = gui.GitCommand.WithSpan(gui.Tr.Spans.StageAllFiles).StageAll()
}
if err != nil {
2020-03-28 02:47:54 +02:00
_ = gui.surfaceError(err)
}
if err := gui.refreshSidePanels(refreshOptions{scope: []RefreshableView{FILES}}); err != nil {
return err
}
return gui.State.Contexts.Files.HandleFocus()
}
2021-03-16 00:07:00 +02:00
func (gui *Gui) handleIgnoreFile() error {
node := gui.getSelectedFileNode()
2021-03-16 00:07:00 +02:00
if node == nil {
2020-08-16 09:45:12 +02:00
return nil
2018-08-14 11:05:26 +02:00
}
2021-03-16 00:07:00 +02:00
if node.GetPath() == ".gitignore" {
2020-10-06 02:52:10 +02:00
return gui.createErrorPanel("Cannot ignore .gitignore")
}
2021-04-11 11:35:42 +02:00
gitCommand := gui.GitCommand.WithSpan(gui.Tr.Spans.IgnoreFile)
2021-03-16 00:07:00 +02:00
unstageFiles := func() error {
return node.ForEachFile(func(file *models.File) error {
if file.HasStagedChanges {
if err := gitCommand.UnStageFile(file.Names(), file.Tracked); err != nil {
2021-03-16 00:07:00 +02:00
return err
}
}
return nil
})
}
if node.GetIsTracked() {
2020-08-15 08:38:16 +02:00
return gui.ask(askOpts{
2020-10-04 02:00:48 +02:00
title: gui.Tr.IgnoreTracked,
prompt: gui.Tr.IgnoreTrackedPrompt,
2020-08-15 08:36:39 +02:00
handleConfirm: func() error {
2021-03-16 00:07:00 +02:00
// not 100% sure if this is necessary but I'll assume it is
if err := unstageFiles(); err != nil {
return err
}
2021-03-16 00:07:00 +02:00
if err := gitCommand.RemoveTrackedFiles(node.GetPath()); err != nil {
2021-03-16 00:07:00 +02:00
return err
}
if err := gitCommand.Ignore(node.GetPath()); err != nil {
return err
}
return gui.refreshSidePanels(refreshOptions{scope: []RefreshableView{FILES}})
2020-08-15 08:36:39 +02:00
},
})
2018-08-14 11:05:26 +02:00
}
2021-03-16 00:07:00 +02:00
if err := unstageFiles(); err != nil {
return err
}
if err := gitCommand.Ignore(node.GetPath()); err != nil {
2020-03-28 02:47:54 +02:00
return gui.surfaceError(err)
2018-08-19 12:41:04 +02:00
}
return gui.refreshSidePanels(refreshOptions{scope: []RefreshableView{FILES}})
2018-08-14 11:05:26 +02:00
}
func (gui *Gui) handleWIPCommitPress() error {
2021-12-29 02:50:20 +02:00
skipHookPrefix := gui.UserConfig.Git.SkipHookPrefix
if skipHookPrefix == "" {
2020-10-04 02:00:48 +02:00
return gui.createErrorPanel(gui.Tr.SkipHookPrefixNotConfigured)
}
2021-10-17 04:00:44 +02:00
textArea := gui.Views.CommitMessage.TextArea
textArea.Clear()
textArea.TypeString(skipHookPrefix)
gui.Views.CommitMessage.RenderTextArea()
2020-08-15 09:23:16 +02:00
return gui.handleCommitPress()
}
2020-10-03 06:54:55 +02:00
func (gui *Gui) commitPrefixConfigForRepo() *config.CommitPrefixConfig {
2021-12-29 02:50:20 +02:00
cfg, ok := gui.UserConfig.Git.CommitPrefixes[utils.GetCurrentRepoName()]
2020-10-03 06:54:55 +02:00
if !ok {
return nil
}
return &cfg
}
2020-11-23 10:14:15 +02:00
func (gui *Gui) prepareFilesForCommit() error {
noStagedFiles := len(gui.stagedFiles()) == 0
2021-12-29 02:50:20 +02:00
if noStagedFiles && gui.UserConfig.Gui.SkipNoStagedFilesWarning {
2021-04-11 11:35:42 +02:00
err := gui.GitCommand.WithSpan(gui.Tr.Spans.StageAllFiles).StageAll()
2020-11-23 10:14:15 +02:00
if err != nil {
return err
}
return gui.refreshFilesAndSubmodules()
}
2020-11-23 10:14:15 +02:00
return nil
}
2020-08-15 09:23:16 +02:00
func (gui *Gui) handleCommitPress() error {
2020-11-23 10:14:15 +02:00
if err := gui.prepareFilesForCommit(); err != nil {
return gui.surfaceError(err)
}
if gui.State.FileManager.GetItemsLength() == 0 {
return gui.createErrorPanel(gui.Tr.NoFilesStagedTitle)
}
2020-11-23 10:14:15 +02:00
if len(gui.stagedFiles()) == 0 {
2020-11-16 11:38:26 +02:00
return gui.promptToStageAllAndRetry(gui.handleCommitPress)
2018-08-14 11:05:26 +02:00
}
2020-10-03 06:54:55 +02:00
commitPrefixConfig := gui.commitPrefixConfigForRepo()
if commitPrefixConfig != nil {
prefixPattern := commitPrefixConfig.Pattern
prefixReplace := commitPrefixConfig.Replace
rgx, err := regexp.Compile(prefixPattern)
if err != nil {
2020-10-04 02:00:48 +02:00
return gui.createErrorPanel(fmt.Sprintf("%s: %s", gui.Tr.LcCommitPrefixPatternError, err.Error()))
}
prefix := rgx.ReplaceAllString(gui.getCheckedOutBranch().Name, prefixReplace)
2021-10-26 00:39:28 +02:00
gui.Views.CommitMessage.ClearTextArea()
gui.Views.CommitMessage.TextArea.TypeString(prefix)
gui.render()
}
2020-08-15 09:23:16 +02:00
gui.g.Update(func(g *gocui.Gui) error {
2021-04-03 06:56:11 +02:00
if err := gui.pushContext(gui.State.Contexts.CommitMessage); err != nil {
2020-03-09 02:34:10 +02:00
return err
}
2018-09-12 15:20:35 +02:00
gui.RenderCommitLength()
return nil
})
return nil
}
func (gui *Gui) promptToStageAllAndRetry(retry func() error) error {
2020-08-15 08:38:16 +02:00
return gui.ask(askOpts{
2020-10-04 02:00:48 +02:00
title: gui.Tr.NoFilesStagedTitle,
prompt: gui.Tr.NoFilesStagedPrompt,
2020-08-15 08:36:39 +02:00
handleConfirm: func() error {
2021-04-11 11:35:42 +02:00
if err := gui.GitCommand.WithSpan(gui.Tr.Spans.StageAllFiles).StageAll(); err != nil {
return gui.surfaceError(err)
}
2020-09-30 00:27:23 +02:00
if err := gui.refreshFilesAndSubmodules(); err != nil {
return gui.surfaceError(err)
}
return retry()
2020-08-15 08:36:39 +02:00
},
})
}
2020-08-15 09:23:16 +02:00
func (gui *Gui) handleAmendCommitPress() error {
if gui.State.FileManager.GetItemsLength() == 0 {
return gui.createErrorPanel(gui.Tr.NoFilesStagedTitle)
}
if len(gui.stagedFiles()) == 0 {
2020-11-16 11:38:26 +02:00
return gui.promptToStageAllAndRetry(gui.handleAmendCommitPress)
2018-09-12 15:20:35 +02:00
}
if len(gui.State.Commits) == 0 {
2020-10-04 02:00:48 +02:00
return gui.createErrorPanel(gui.Tr.NoCommitToAmend)
}
2020-08-15 08:38:16 +02:00
return gui.ask(askOpts{
2020-10-04 02:00:48 +02:00
title: strings.Title(gui.Tr.AmendLastCommit),
prompt: gui.Tr.SureToAmend,
2020-08-15 08:36:39 +02:00
handleConfirm: func() error {
2021-12-07 12:59:36 +02:00
cmdObj := gui.GitCommand.AmendHeadCmdObj()
gui.OnRunCommand(oscommands.NewCmdLogEntry(cmdObj.ToString(), gui.Tr.Spans.AmendCommit, true))
return gui.withGpgHandling(cmdObj, gui.Tr.AmendingStatus, nil)
2020-08-15 08:36:39 +02:00
},
})
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
2020-08-15 09:23:16 +02:00
func (gui *Gui) handleCommitEditorPress() error {
if gui.State.FileManager.GetItemsLength() == 0 {
return gui.createErrorPanel(gui.Tr.NoFilesStagedTitle)
}
if len(gui.stagedFiles()) == 0 {
2020-11-16 11:38:26 +02:00
return gui.promptToStageAllAndRetry(gui.handleCommitEditorPress)
2018-08-14 11:05:26 +02:00
}
args := []string{"commit"}
2021-12-29 02:50:20 +02:00
if gui.UserConfig.Git.Commit.SignOff {
args = append(args, "--signoff")
}
2021-12-07 12:59:36 +02:00
cmdStr := "git " + strings.Join(args, " ")
2021-04-10 03:40:42 +02:00
return gui.runSubprocessWithSuspenseAndRefresh(
2021-12-29 05:33:38 +02:00
gui.GitCommand.WithSpan(gui.Tr.Spans.Commit).Cmd.New(cmdStr).Log(),
)
2021-04-01 11:10:24 +02:00
}
func (gui *Gui) handleStatusFilterPressed() error {
2021-08-03 22:47:11 +02:00
menuItems := []*menuItem{
2021-08-03 22:44:43 +02:00
{
displayString: gui.Tr.FilterStagedFiles,
onPress: func() error {
return gui.setStatusFiltering(filetree.DisplayStaged)
},
},
2021-08-03 22:44:43 +02:00
{
displayString: gui.Tr.FilterUnstagedFiles,
onPress: func() error {
return gui.setStatusFiltering(filetree.DisplayUnstaged)
},
},
2021-08-03 22:44:43 +02:00
{
displayString: gui.Tr.ResetCommitFilterState,
onPress: func() error {
return gui.setStatusFiltering(filetree.DisplayAll)
},
},
2021-08-03 22:44:43 +02:00
}
2021-08-03 22:47:11 +02:00
return gui.createMenu(gui.Tr.FilteringMenuTitle, menuItems, createMenuOptions{showCancel: true})
}
func (gui *Gui) setStatusFiltering(filter filetree.FileManagerDisplayFilter) error {
state := gui.State
state.FileManager.SetDisplayFilter(filter)
return gui.handleRefreshFiles()
}
func (gui *Gui) editFile(filename string) error {
return gui.editFileAtLine(filename, 1)
}
func (gui *Gui) editFileAtLine(filename string, lineNumber int) error {
cmdStr, err := gui.GitCommand.EditFileCmdStr(filename, lineNumber)
2021-04-10 03:40:42 +02:00
if err != nil {
return gui.surfaceError(err)
}
return gui.runSubprocessWithSuspenseAndRefresh(
2021-12-29 05:33:38 +02:00
gui.OSCommand.WithSpan(gui.Tr.Spans.EditFile).Cmd.NewShell(cmdStr),
2021-04-10 03:40:42 +02:00
)
2018-08-14 11:05:26 +02:00
}
func (gui *Gui) handleFileEdit() error {
node := gui.getSelectedFileNode()
2021-03-15 14:00:20 +02:00
if node == nil {
2020-08-16 09:45:12 +02:00
return nil
}
2021-03-15 14:00:20 +02:00
if node.File == nil {
return gui.createErrorPanel(gui.Tr.ErrCannotEditDirectory)
}
return gui.editFile(node.GetPath())
2018-08-14 11:05:26 +02:00
}
func (gui *Gui) handleFileOpen() error {
node := gui.getSelectedFileNode()
2021-03-15 14:00:20 +02:00
if node == nil {
2020-08-16 09:45:12 +02:00
return nil
}
2021-03-15 14:00:20 +02:00
return gui.openFile(node.GetPath())
2018-08-14 11:05:26 +02:00
}
func (gui *Gui) handleRefreshFiles() error {
return gui.refreshSidePanels(refreshOptions{scope: []RefreshableView{FILES}})
2018-08-14 11:05:26 +02:00
}
2019-03-02 04:22:02 +02:00
func (gui *Gui) refreshStateFiles() error {
2021-04-03 10:35:45 +02:00
state := gui.State
// keep track of where the cursor is currently and the current file names
// when we refresh, go looking for a matching name
// move the cursor to there.
2021-03-21 00:06:15 +02:00
selectedNode := gui.getSelectedFileNode()
2021-03-21 00:06:15 +02:00
prevNodes := gui.State.FileManager.GetAllItems()
prevSelectedLineIdx := gui.State.Panels.Files.SelectedLineIdx
2021-03-20 23:41:06 +02:00
files := gui.GitCommand.GetStatusFiles(commands.GetStatusFileOptions{})
2021-03-21 07:28:03 +02:00
// for when you stage the old file of a rename and the new file is in a collapsed dir
2021-04-03 10:35:45 +02:00
state.FileManager.RWMutex.Lock()
2021-03-21 07:28:03 +02:00
for _, file := range files {
if selectedNode != nil && selectedNode.Path != "" && file.PreviousName == selectedNode.Path {
2021-04-03 10:35:45 +02:00
state.FileManager.ExpandToPath(file.Name)
2021-03-21 07:28:03 +02:00
}
}
2021-04-03 10:35:45 +02:00
state.FileManager.SetFiles(files)
state.FileManager.RWMutex.Unlock()
if err := gui.fileWatcher.addFilesToFileWatcher(files); err != nil {
return err
}
2021-03-21 00:06:15 +02:00
if selectedNode != nil {
2021-04-03 10:35:45 +02:00
newIdx := gui.findNewSelectedIdx(prevNodes[prevSelectedLineIdx:], state.FileManager.GetAllItems())
2021-03-21 00:37:16 +02:00
if newIdx != -1 && newIdx != prevSelectedLineIdx {
2021-04-03 10:35:45 +02:00
newNode := state.FileManager.GetItemAtIndex(newIdx)
// when not in tree mode, we show merge conflict files at the top, so you
// can work through them one by one without having to sift through a large
// set of files. If you have just fixed the merge conflicts of a file, we
// actually don't want to jump to that file's new position, because that
// file will now be ages away amidst the other files without merge
// conflicts: the user in this case would rather work on the next file
// with merge conflicts, which will have moved up to fill the gap left by
// the last file, meaning the cursor doesn't need to move at all.
2021-04-03 10:35:45 +02:00
leaveCursor := !state.FileManager.InTreeMode() && newNode != nil &&
selectedNode.File != nil && selectedNode.File.HasMergeConflicts &&
newNode.File != nil && !newNode.File.HasMergeConflicts
if !leaveCursor {
2021-04-03 10:35:45 +02:00
state.Panels.Files.SelectedLineIdx = newIdx
}
2021-03-21 00:37:16 +02:00
}
}
2021-04-03 10:35:45 +02:00
gui.refreshSelectedLine(state.Panels.Files, state.FileManager.GetItemsLength())
2021-03-21 00:37:16 +02:00
return nil
}
// Let's try to find our file again and move the cursor to that.
// If we can't find our file, it was probably just removed by the user. In that
// case, we go looking for where the next file has been moved to. Given that the
// user could have removed a whole directory, we continue iterating through the old
// nodes until we find one that exists in the new set of nodes, then move the cursor
// to that.
// prevNodes starts from our previously selected node because we don't need to consider anything above that
func (gui *Gui) findNewSelectedIdx(prevNodes []*filetree.FileNode, currNodes []*filetree.FileNode) int {
getPaths := func(node *filetree.FileNode) []string {
2021-03-21 00:37:16 +02:00
if node == nil {
return nil
}
if node.File != nil && node.File.IsRename() {
return node.File.Names()
} else {
return []string{node.Path}
2021-03-21 00:20:52 +02:00
}
2021-03-21 00:37:16 +02:00
}
2021-03-21 00:20:52 +02:00
2021-03-21 00:37:16 +02:00
for _, prevNode := range prevNodes {
selectedPaths := getPaths(prevNode)
2021-03-21 00:20:52 +02:00
2021-03-21 00:37:16 +02:00
for idx, node := range currNodes {
paths := getPaths(node)
2021-03-21 00:20:52 +02:00
2021-03-21 00:37:16 +02:00
// If you started off with a rename selected, and now it's broken in two, we want you to jump to the new file, not the old file.
// This is because the new should be in the same position as the rename was meaning less cursor jumping
foundOldFileInRename := prevNode.File != nil && prevNode.File.IsRename() && node.Path == prevNode.File.PreviousName
foundNode := utils.StringArraysOverlap(paths, selectedPaths) && !foundOldFileInRename
if foundNode {
return idx
}
}
}
2021-03-21 00:37:16 +02:00
return -1
2018-08-14 11:05:26 +02:00
}
func (gui *Gui) handlePullFiles() error {
2020-08-23 03:13:51 +02:00
if gui.popupPanelFocused() {
return nil
}
2021-04-11 11:35:42 +02:00
span := gui.Tr.Spans.Pull
2021-04-10 09:31:23 +02:00
currentBranch := gui.currentBranch()
2020-08-23 09:28:28 +02:00
if currentBranch == nil {
// need to wait for branches to refresh
return nil
}
// if we have no upstream branch we need to set that first
2021-06-05 07:56:50 +02:00
if !currentBranch.IsTrackingRemote() {
// see if we have this branch in our config with an upstream
conf, err := gui.GitCommand.Repo.Config()
if err != nil {
2020-03-28 02:47:54 +02:00
return gui.surfaceError(err)
}
for branchName, branch := range conf.Branches {
if branchName == currentBranch.Name {
2021-04-10 09:31:23 +02:00
return gui.pullFiles(PullFilesOptions{RemoteName: branch.Remote, BranchName: branch.Name, span: span})
}
}
suggestedRemote := getSuggestedRemote(gui.State.Remotes)
2020-11-28 04:35:58 +02:00
return gui.prompt(promptOpts{
2021-10-23 02:25:37 +02:00
title: gui.Tr.EnterUpstream,
initialContent: suggestedRemote + "/" + currentBranch.Name,
2021-10-23 02:25:37 +02:00
findSuggestionsFunc: gui.getRemoteBranchesSuggestionsFunc("/"),
2020-11-28 04:35:58 +02:00
handleConfirm: func(upstream string) error {
if err := gui.GitCommand.SetUpstreamBranch(upstream); err != nil {
errorMessage := err.Error()
if strings.Contains(errorMessage, "does not exist") {
errorMessage = fmt.Sprintf("upstream branch %s not found.\nIf you expect it to exist, you should fetch (with 'f').\nOtherwise, you should push (with 'shift+P')", upstream)
}
return gui.createErrorPanel(errorMessage)
}
2021-04-10 09:31:23 +02:00
return gui.pullFiles(PullFilesOptions{span: span})
2020-11-28 04:35:58 +02:00
},
})
}
2021-04-10 09:31:23 +02:00
return gui.pullFiles(PullFilesOptions{span: span})
}
2020-08-11 13:18:38 +02:00
type PullFilesOptions struct {
2021-10-20 13:21:16 +02:00
RemoteName string
BranchName string
FastForwardOnly bool
span string
2020-08-11 13:18:38 +02:00
}
func (gui *Gui) pullFiles(opts PullFilesOptions) error {
2020-11-16 11:38:26 +02:00
if err := gui.createLoaderPanel(gui.Tr.PullWait); err != nil {
return err
}
2019-02-16 03:03:22 +02:00
// TODO: this doesn't look like a good idea. Why the goroutine?
2021-10-20 13:21:16 +02:00
go utils.Safe(func() { _ = gui.pullWithLock(opts) })
2018-08-14 11:05:26 +02:00
return nil
}
2021-10-20 13:21:16 +02:00
func (gui *Gui) pullWithLock(opts PullFilesOptions) error {
gui.Mutexes.FetchMutex.Lock()
defer gui.Mutexes.FetchMutex.Unlock()
2020-08-23 13:25:39 +02:00
2021-04-10 09:31:23 +02:00
gitCommand := gui.GitCommand.WithSpan(opts.span)
2021-10-20 13:21:16 +02:00
err := gitCommand.Pull(
commands.PullOptions{
2020-08-11 13:38:59 +02:00
PromptUserForCredential: gui.promptUserForCredential,
RemoteName: opts.RemoteName,
BranchName: opts.BranchName,
2021-10-20 13:21:16 +02:00
FastForwardOnly: opts.FastForwardOnly,
2020-08-11 13:38:59 +02:00
},
)
2021-10-20 13:21:16 +02:00
if err == nil {
_ = gui.closeConfirmationPrompt(false)
2020-08-11 13:38:59 +02:00
}
2021-10-20 13:21:16 +02:00
return gui.handleGenericMergeCommandResult(err)
2020-08-11 13:38:59 +02:00
}
type pushOpts struct {
force bool
upstreamRemote string
upstreamBranch string
setUpstream bool
}
func (gui *Gui) push(opts pushOpts) error {
2020-11-16 11:38:26 +02:00
if err := gui.createLoaderPanel(gui.Tr.PushWait); err != nil {
return err
}
2020-10-07 12:19:38 +02:00
go utils.Safe(func() {
err := gui.GitCommand.WithSpan(gui.Tr.Spans.Push).Push(commands.PushOpts{
Force: opts.force,
UpstreamRemote: opts.upstreamRemote,
UpstreamBranch: opts.upstreamBranch,
SetUpstream: opts.setUpstream,
PromptUserForCredential: gui.promptUserForCredential,
})
if err != nil && !opts.force && strings.Contains(err.Error(), "Updates were rejected") {
2021-12-29 02:50:20 +02:00
forcePushDisabled := gui.UserConfig.Git.DisableForcePushing
if forcePushDisabled {
2020-11-16 11:38:26 +02:00
_ = gui.createErrorPanel(gui.Tr.UpdatesRejectedAndForcePushDisabled)
return
}
2020-11-16 11:38:26 +02:00
_ = gui.ask(askOpts{
2020-10-04 02:00:48 +02:00
title: gui.Tr.ForcePush,
prompt: gui.Tr.ForcePushPrompt,
2020-08-15 08:36:39 +02:00
handleConfirm: func() error {
newOpts := opts
newOpts.force = true
return gui.push(newOpts)
2020-08-15 08:36:39 +02:00
},
})
2020-08-12 12:52:40 +02:00
return
}
2020-08-11 13:41:32 +02:00
gui.handleCredentialsPopup(err)
2020-08-11 13:29:18 +02:00
_ = gui.refreshSidePanels(refreshOptions{mode: ASYNC})
2020-10-07 12:19:38 +02:00
})
2018-08-14 11:05:26 +02:00
return nil
}
func (gui *Gui) pushFiles() error {
2020-08-23 03:13:51 +02:00
if gui.popupPanelFocused() {
return nil
}
// if we have pullables we'll ask if the user wants to force push
currentBranch := gui.currentBranch()
2021-04-08 16:33:39 +02:00
if currentBranch == nil {
// need to wait for branches to refresh
return nil
}
2021-06-05 07:56:50 +02:00
if currentBranch.IsTrackingRemote() {
if currentBranch.HasCommitsToPull() {
return gui.requestToForcePush()
} else {
return gui.push(pushOpts{})
2021-06-05 07:56:50 +02:00
}
} else {
// see if we have an upstream for this branch in our config
upstreamRemote, upstreamBranch, err := gui.upstreamForBranchInConfig(currentBranch.Name)
if err != nil {
2020-03-28 02:47:54 +02:00
return gui.surfaceError(err)
}
2021-06-05 07:56:50 +02:00
if upstreamBranch != "" {
return gui.push(
pushOpts{
force: false,
upstreamRemote: upstreamRemote,
upstreamBranch: upstreamBranch,
},
)
}
suggestedRemote := getSuggestedRemote(gui.State.Remotes)
if gui.GitCommand.PushToCurrent {
return gui.push(pushOpts{setUpstream: true})
} else {
2020-11-28 04:35:58 +02:00
return gui.prompt(promptOpts{
2021-10-23 02:25:37 +02:00
title: gui.Tr.EnterUpstream,
initialContent: suggestedRemote + " " + currentBranch.Name,
2021-10-23 02:25:37 +02:00
findSuggestionsFunc: gui.getRemoteBranchesSuggestionsFunc(" "),
2021-06-05 07:56:50 +02:00
handleConfirm: func(upstream string) error {
var upstreamBranch, upstreamRemote string
split := strings.Split(upstream, " ")
if len(split) == 2 {
upstreamRemote = split[0]
upstreamBranch = split[1]
} else {
upstreamRemote = upstream
upstreamBranch = ""
}
return gui.push(pushOpts{
force: false,
upstreamRemote: upstreamRemote,
upstreamBranch: upstreamBranch,
setUpstream: true,
})
2020-11-28 04:35:58 +02:00
},
})
}
}
2021-06-05 07:56:50 +02:00
}
2020-08-15 08:36:39 +02:00
func getSuggestedRemote(remotes []*models.Remote) string {
if len(remotes) == 0 {
return "origin"
}
for _, remote := range remotes {
if remote.Name == "origin" {
return remote.Name
}
}
return remotes[0].Name
}
2021-06-05 07:56:50 +02:00
func (gui *Gui) requestToForcePush() error {
2021-12-29 02:50:20 +02:00
forcePushDisabled := gui.UserConfig.Git.DisableForcePushing
if forcePushDisabled {
2020-10-04 02:00:48 +02:00
return gui.createErrorPanel(gui.Tr.ForcePushDisabled)
}
2020-08-15 08:38:16 +02:00
return gui.ask(askOpts{
2020-10-04 02:00:48 +02:00
title: gui.Tr.ForcePush,
prompt: gui.Tr.ForcePushPrompt,
2020-08-15 08:36:39 +02:00
handleConfirm: func() error {
return gui.push(pushOpts{force: true})
2020-08-15 08:36:39 +02:00
},
})
}
func (gui *Gui) upstreamForBranchInConfig(branchName string) (string, string, error) {
2021-06-05 07:56:50 +02:00
conf, err := gui.GitCommand.Repo.Config()
if err != nil {
return "", "", err
2021-06-05 07:56:50 +02:00
}
for configBranchName, configBranch := range conf.Branches {
if configBranchName == branchName {
return configBranch.Remote, configBranchName, nil
2021-06-05 07:56:50 +02:00
}
}
return "", "", nil
2021-06-05 07:56:50 +02:00
}
2020-08-15 09:23:16 +02:00
func (gui *Gui) handleSwitchToMerge() error {
2020-08-16 09:45:12 +02:00
file := gui.getSelectedFile()
if file == nil {
2018-08-14 11:05:26 +02:00
return nil
}
2020-08-16 09:45:12 +02:00
if !file.HasInlineMergeConflicts {
2020-10-04 02:00:48 +02:00
return gui.createErrorPanel(gui.Tr.FileNoMergeCons)
2018-08-14 11:05:26 +02:00
}
2020-08-23 03:08:51 +02:00
2021-04-03 06:56:11 +02:00
return gui.pushContext(gui.State.Contexts.Merging)
2018-08-14 11:05:26 +02:00
}
2018-08-23 21:05:09 +02:00
func (gui *Gui) openFile(filename string) error {
2021-04-11 11:35:42 +02:00
if err := gui.OSCommand.WithSpan(gui.Tr.Spans.OpenFile).OpenFile(filename); err != nil {
2020-03-28 02:47:54 +02:00
return gui.surfaceError(err)
2018-08-23 21:05:09 +02:00
}
return nil
}
2018-12-08 07:54:54 +02:00
func (gui *Gui) anyFilesWithMergeConflicts() bool {
for _, file := range gui.State.FileManager.GetAllFiles() {
2018-12-08 07:54:54 +02:00
if file.HasMergeConflicts {
return true
}
}
return false
}
func (gui *Gui) handleCustomCommand() error {
2020-11-28 04:35:58 +02:00
return gui.prompt(promptOpts{
2021-10-23 02:25:37 +02:00
title: gui.Tr.CustomCommand,
findSuggestionsFunc: gui.getCustomCommandsHistorySuggestionsFunc(),
2020-11-28 04:35:58 +02:00
handleConfirm: func(command string) error {
2021-10-23 02:25:37 +02:00
gui.Config.GetAppState().CustomCommandsHistory = utils.Limit(
utils.Uniq(
append(gui.Config.GetAppState().CustomCommandsHistory, command),
),
1000,
)
err := gui.Config.SaveAppState()
if err != nil {
gui.Log.Error(err)
}
2021-04-11 11:35:42 +02:00
gui.OnRunCommand(oscommands.NewCmdLogEntry(command, gui.Tr.Spans.CustomCommand, true))
2021-04-10 03:40:42 +02:00
return gui.runSubprocessWithSuspenseAndRefresh(
2021-12-29 05:33:38 +02:00
gui.OSCommand.Cmd.NewShell(command),
)
2020-11-28 04:35:58 +02:00
},
2019-03-12 12:43:56 +02:00
})
}
func (gui *Gui) handleCreateStashMenu() error {
2020-02-14 14:36:55 +02:00
menuItems := []*menuItem{
{
2020-10-04 02:00:48 +02:00
displayString: gui.Tr.LcStashAllChanges,
2020-02-14 14:36:55 +02:00
onPress: func() error {
2021-04-11 11:35:42 +02:00
return gui.handleStashSave(gui.GitCommand.WithSpan(gui.Tr.Spans.StashAllChanges).StashSave)
},
},
{
2020-10-04 02:00:48 +02:00
displayString: gui.Tr.LcStashStagedChanges,
2020-02-14 14:36:55 +02:00
onPress: func() error {
2021-04-11 11:35:42 +02:00
return gui.handleStashSave(gui.GitCommand.WithSpan(gui.Tr.Spans.StashStagedChanges).StashSaveStagedChanges)
},
},
}
2020-10-04 02:00:48 +02:00
return gui.createMenu(gui.Tr.LcStashOptions, menuItems, createMenuOptions{showCancel: true})
}
func (gui *Gui) handleStashChanges() error {
return gui.handleStashSave(gui.GitCommand.StashSave)
}
func (gui *Gui) handleCreateResetToUpstreamMenu() error {
return gui.createResetMenu("@{upstream}")
}
2021-03-14 11:18:06 +02:00
func (gui *Gui) handleToggleDirCollapsed() error {
node := gui.getSelectedFileNode()
2021-03-14 11:18:06 +02:00
if node == nil {
return nil
}
gui.State.FileManager.ToggleCollapsed(node.GetPath())
2021-03-14 11:18:06 +02:00
2021-04-03 06:56:11 +02:00
if err := gui.postRefreshUpdate(gui.State.Contexts.Files); err != nil {
2021-03-14 11:18:06 +02:00
gui.Log.Error(err)
}
return nil
}
2021-03-20 23:41:06 +02:00
func (gui *Gui) handleToggleFileTreeView() error {
// get path of currently selected file
path := gui.getSelectedPath()
2021-03-21 06:28:18 +02:00
gui.State.FileManager.ToggleShowTree()
2021-03-20 23:41:06 +02:00
// find that same node in the new format and move the cursor to it
if path != "" {
gui.State.FileManager.ExpandToPath(path)
index, found := gui.State.FileManager.GetIndexForPath(path)
2021-03-20 23:41:06 +02:00
if found {
gui.filesListContext().GetPanelState().SetSelectedLineIdx(index)
}
}
2021-04-04 17:10:23 +02:00
if ContextKey(gui.Views.Files.Context) == FILES_CONTEXT_KEY {
2021-04-03 06:56:11 +02:00
if err := gui.State.Contexts.Files.HandleRender(); err != nil {
2021-03-20 23:41:06 +02:00
return err
}
2021-04-03 06:56:11 +02:00
if err := gui.State.Contexts.Files.HandleFocus(); err != nil {
return err
}
2021-03-20 23:41:06 +02:00
}
return nil
}
2021-04-11 02:05:39 +02:00
func (gui *Gui) handleOpenMergeTool() error {
return gui.ask(askOpts{
title: gui.Tr.MergeToolTitle,
prompt: gui.Tr.MergeToolPrompt,
handleConfirm: func() error {
return gui.runSubprocessWithSuspenseAndRefresh(
2021-12-07 12:59:36 +02:00
gui.GitCommand.OpenMergeToolCmdObj(),
2021-04-11 02:05:39 +02:00
)
},
})
}