From 6349214f000fcece292fb7e1176fee833adcdf59 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Fri, 17 Jul 2020 08:39:45 +1000 Subject: [PATCH] prompt to commit all files if committing with no staged files --- pkg/gui/files_panel.go | 36 ++++++++++++++++++++++++++++++------ pkg/i18n/dutch.go | 9 ++++++--- pkg/i18n/english.go | 9 ++++++--- pkg/i18n/polish.go | 9 ++++++--- 4 files changed, 48 insertions(+), 15 deletions(-) diff --git a/pkg/gui/files_panel.go b/pkg/gui/files_panel.go index cb3355fc3..034a21fb6 100644 --- a/pkg/gui/files_panel.go +++ b/pkg/gui/files_panel.go @@ -284,9 +284,12 @@ func (gui *Gui) handleWIPCommitPress(g *gocui.Gui, filesView *gocui.View) error } func (gui *Gui) handleCommitPress(g *gocui.Gui, filesView *gocui.View) error { - if len(gui.stagedFiles()) == 0 && gui.GitCommand.WorkingTreeState() == "normal" { - return gui.createErrorPanel(gui.Tr.SLocalize("NoStagedFilesToCommit")) + if len(gui.stagedFiles()) == 0 { + return gui.promptToStageAllAndRetry(func() error { + return gui.handleCommitPress(gui.g, filesView) + }) } + commitMessageView := gui.getCommitMessageView() prefixPattern := gui.Config.GetUserConfig().GetString("git.commitPrefixes." + utils.GetCurrentRepoName() + ".pattern") prefixReplace := gui.Config.GetUserConfig().GetString("git.commitPrefixes." + utils.GetCurrentRepoName() + ".replace") @@ -317,10 +320,28 @@ func (gui *Gui) handleCommitPress(g *gocui.Gui, filesView *gocui.View) error { return nil } +func (gui *Gui) promptToStageAllAndRetry(retry func() error) error { + return gui.createConfirmationPanel( + gui.g, gui.getFilesView(), true, gui.Tr.SLocalize("NoFilesStagedTitle"), gui.Tr.SLocalize("NoFilesStagedPrompt"), + func(*gocui.Gui, *gocui.View) error { + if err := gui.GitCommand.StageAll(); err != nil { + return gui.surfaceError(err) + } + if err := gui.refreshFiles(); err != nil { + return gui.surfaceError(err) + } + + return retry() + }, nil) +} + func (gui *Gui) handleAmendCommitPress(g *gocui.Gui, filesView *gocui.View) error { - if len(gui.stagedFiles()) == 0 && gui.GitCommand.WorkingTreeState() == "normal" { - return gui.createErrorPanel(gui.Tr.SLocalize("NoStagedFilesToCommit")) + if len(gui.stagedFiles()) == 0 { + return gui.promptToStageAllAndRetry(func() error { + return gui.handleAmendCommitPress(gui.g, filesView) + }) } + if len(gui.State.Commits) == 0 { return gui.createErrorPanel(gui.Tr.SLocalize("NoCommitToAmend")) } @@ -344,9 +365,12 @@ func (gui *Gui) handleAmendCommitPress(g *gocui.Gui, filesView *gocui.View) erro // 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.GitCommand.WorkingTreeState() == "normal" { - return gui.createErrorPanel(gui.Tr.SLocalize("NoStagedFilesToCommit")) + if len(gui.stagedFiles()) == 0 { + return gui.promptToStageAllAndRetry(func() error { + return gui.handleCommitEditorPress(gui.g, filesView) + }) } + gui.PrepareSubProcess(g, "git", "commit") return nil } diff --git a/pkg/i18n/dutch.go b/pkg/i18n/dutch.go index f4ab0c768..f4cecf4c8 100644 --- a/pkg/i18n/dutch.go +++ b/pkg/i18n/dutch.go @@ -136,9 +136,6 @@ func addDutch(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "CannotGitAdd", Other: "Kan commando niet uitvoeren git add --path untracked files", - }, &i18n.Message{ - ID: "NoStagedFilesToCommit", - Other: "Er zijn geen staged bestanden om te commiten", }, &i18n.Message{ ID: "NoFilesDisplay", Other: "Geen bestanden om te laten zien", @@ -766,6 +763,12 @@ func addDutch(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "commitPrefixPatternError", Other: "Error in commitPrefix pattern", + }, &i18n.Message{ + ID: "NoFilesStagedTitle", + Other: "No files staged", + }, &i18n.Message{ + ID: "NoFilesStagedPrompt", + Other: "You have not staged any files. Commit all files?", }, ) } diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 6bca0589d..e7a16484a 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -156,9 +156,6 @@ func addEnglish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "CannotGitAdd", Other: "Cannot git add --patch untracked files", - }, &i18n.Message{ - ID: "NoStagedFilesToCommit", - Other: "There are no staged files to commit", }, &i18n.Message{ ID: "NoFilesDisplay", Other: "No file to display", @@ -1152,6 +1149,12 @@ func addEnglish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "commitPrefixPatternError", Other: "Error in commitPrefix pattern", + }, &i18n.Message{ + ID: "NoFilesStagedTitle", + Other: "No files staged", + }, &i18n.Message{ + ID: "NoFilesStagedPrompt", + Other: "You have not staged any files. Commit all files?", }, ) } diff --git a/pkg/i18n/polish.go b/pkg/i18n/polish.go index e42d726e2..54ba3bd2c 100644 --- a/pkg/i18n/polish.go +++ b/pkg/i18n/polish.go @@ -128,9 +128,6 @@ func addPolish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "CannotGitAdd", Other: "Nie można git add --patch nieśledzonych plików", - }, &i18n.Message{ - ID: "NoStagedFilesToCommit", - Other: "Brak zatwierdzonych plików do commita", }, &i18n.Message{ ID: "NoFilesDisplay", Other: "Brak pliku do wyświetlenia", @@ -749,6 +746,12 @@ func addPolish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "commitPrefixPatternError", Other: "Error in commitPrefix pattern", + }, &i18n.Message{ + ID: "NoFilesStagedTitle", + Other: "No files staged", + }, &i18n.Message{ + ID: "NoFilesStagedPrompt", + Other: "You have not staged any files. Commit all files?", }, ) }