summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2019-03-16 12:45:38 +1100
committerJesse Duffield <jessedduffield@gmail.com>2019-03-16 12:51:48 +1100
commite583cc251984deead48c062f19c3d88d88236eda (patch)
tree1ba9dbef96e90dc561c11efb42609d392d515cf9
parent0d208b7957faa098f9f462cfe076ed90e5bf05dd (diff)
allow autostashing changes when checking out a branch
-rw-r--r--pkg/gui/branches_panel.go50
-rw-r--r--pkg/i18n/dutch.go9
-rw-r--r--pkg/i18n/english.go9
-rw-r--r--pkg/i18n/polish.go9
-rwxr-xr-xtest/repos/merge_conflict.sh21
5 files changed, 84 insertions, 14 deletions
diff --git a/pkg/gui/branches_panel.go b/pkg/gui/branches_panel.go
index 5024e2b9d..8ee1b451d 100644
--- a/pkg/gui/branches_panel.go
+++ b/pkg/gui/branches_panel.go
@@ -121,15 +121,7 @@ func (gui *Gui) handleBranchPress(g *gocui.Gui, v *gocui.View) error {
return gui.createErrorPanel(g, gui.Tr.SLocalize("AlreadyCheckedOutBranch"))
}
branch := gui.getSelectedBranch()
- if err := gui.GitCommand.Checkout(branch.Name, false); err != nil {
- if err := gui.createErrorPanel(g, err.Error()); err != nil {
- return err
- }
- } else {
- gui.State.Panels.Branches.SelectedLine = 0
- }
-
- return gui.refreshSidePanels(g)
+ return gui.handleCheckoutBranch(branch.Name)
}
func (gui *Gui) handleCreatePullRequestPress(g *gocui.Gui, v *gocui.View) error {
@@ -166,12 +158,44 @@ func (gui *Gui) handleForceCheckout(g *gocui.Gui, v *gocui.View) error {
}, nil)
}
+func (gui *Gui) handleCheckoutBranch(branchName string) error {
+ if err := gui.GitCommand.Checkout(branchName, false); err != nil {
+ // note, this will only work for english-language git commands. If we force git to use english, and the error isn't this one, then the user will receive an english command they may not understand. I'm not sure what the best solution to this is. Running the command once in english and a second time in the native language is one option
+ if !strings.Contains(err.Error(), "Please commit your changes or stash them before you switch branch") {
+ if err := gui.createErrorPanel(gui.g, err.Error()); err != nil {
+ return err
+ }
+ }
+
+ // offer to autostash changes
+ return gui.createConfirmationPanel(gui.g, gui.getBranchesView(), gui.Tr.SLocalize("AutoStashTitle"), gui.Tr.SLocalize("AutoStashPrompt"), func(g *gocui.Gui, v *gocui.View) error {
+ if err := gui.GitCommand.StashSave(gui.Tr.SLocalize("StashPrefix") + branchName); err != nil {
+ return gui.createErrorPanel(g, err.Error())
+ }
+ if err := gui.GitCommand.Checkout(branchName, false); err != nil {
+ return gui.createErrorPanel(g, err.Error())
+ }
+
+ // checkout successful so we select the new branch
+ gui.State.Panels.Branches.SelectedLine = 0
+
+ if err := gui.GitCommand.StashDo(0, "pop"); err != nil {
+ if err := gui.refreshSidePanels(g); err != nil {
+ return err
+ }
+ return gui.createErrorPanel(g, err.Error())
+ }
+ return gui.refreshSidePanels(g)
+ }, nil)
+ }
+
+ gui.State.Panels.Branches.SelectedLine = 0
+ return gui.refreshSidePanels(gui.g)
+}
+
func (gui *Gui) handleCheckoutByName(g *gocui.Gui, v *gocui.View) error {
gui.createPromptPanel(g, v, gui.Tr.SLocalize("BranchName")+":", func(g *gocui.Gui, v *gocui.View) error {
- if err := gui.GitCommand.Checkout(gui.trimmedContent(v), false); err != nil {
- return gui.createErrorPanel(g, err.Error())
- }
- return gui.refreshSidePanels(g)
+ return gui.handleCheckoutBranch(gui.trimmedContent(v))
})
return nil
}
diff --git a/pkg/i18n/dutch.go b/pkg/i18n/dutch.go
index 4daa83075..74bf39a97 100644
--- a/pkg/i18n/dutch.go
+++ b/pkg/i18n/dutch.go
@@ -673,6 +673,15 @@ func addDutch(i18nObject *i18n.Bundle) error {
}, &i18n.Message{
ID: "CreateRepo",
Other: "Not in a git repository. Create a new git repository? (y/n): ",
+ }, &i18n.Message{
+ ID: "AutoStashTitle",
+ Other: "Autostash?",
+ }, &i18n.Message{
+ ID: "AutoStashPrompt",
+ Other: "You must stash and pop your changes to bring them across. Do this automatically? (enter/esc)",
+ }, &i18n.Message{
+ ID: "StashPrefix",
+ Other: "Auto-stashing changes for ",
},
)
}
diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go
index b94bbb3ec..9280c0bbd 100644
--- a/pkg/i18n/english.go
+++ b/pkg/i18n/english.go
@@ -696,6 +696,15 @@ func addEnglish(i18nObject *i18n.Bundle) error {
}, &i18n.Message{
ID: "CreateRepo",
Other: "Not in a git repository. Create a new git repository? (y/n): ",
+ }, &i18n.Message{
+ ID: "AutoStashTitle",
+ Other: "Autostash?",
+ }, &i18n.Message{
+ ID: "AutoStashPrompt",
+ Other: "You must stash and pop your changes to bring them across. Do this automatically? (enter/esc)",
+ }, &i18n.Message{
+ ID: "StashPrefix",
+ Other: "Auto-stashing changes for ",
},
)
}
diff --git a/pkg/i18n/polish.go b/pkg/i18n/polish.go
index b459c6ee3..753ef882c 100644
--- a/pkg/i18n/polish.go
+++ b/pkg/i18n/polish.go
@@ -656,6 +656,15 @@ func addPolish(i18nObject *i18n.Bundle) error {
}, &i18n.Message{
ID: "CreateRepo",
Other: "Not in a git repository. Create a new git repository? (y/n): ",
+ }, &i18n.Message{
+ ID: "AutoStashTitle",
+ Other: "Autostash?",
+ }, &i18n.Message{
+ ID: "AutoStashPrompt",
+ Other: "You must stash and pop your changes to bring them across. Do this automatically? (enter/esc)",
+ }, &i18n.Message{
+ ID: "StashPrefix",
+ Other: "Auto-stashing changes for ",
},
)
}
diff --git a/test/repos/merge_conflict.sh b/test/repos/merge_conflict.sh
index 88b5bfc18..7796cf38a 100755
--- a/test/repos/merge_conflict.sh
+++ b/test/repos/merge_conflict.sh
@@ -133,4 +133,23 @@ echo "once upon a time there was a horse" >> file5
git add file5
git commit -m "fourth commit on master"
-# git merge develop # should have a merge conflict here
+
+# this is for the autostash feature
+
+git checkout -b base_branch
+
+echo "original1\noriginal2\noriginal3" > file
+git add file
+git commit -m "file"
+
+git checkout -b other_branch
+
+git checkout base_branch
+
+echo "new1\noriginal2\noriginal3" > file
+git add file
+git commit -m "file changed"
+
+git checkout other_branch
+
+echo "new2\noriginal2\noriginal3" > file