summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorİbrahim Serdar Açıkgöz <serdaracikgoz86@gmail.com>2018-12-07 18:14:21 +0300
committerİbrahim Serdar Açıkgöz <serdaracikgoz86@gmail.com>2018-12-07 18:14:21 +0300
commit80bf9c51de05e876f100bf5edf260f9cbb70cce2 (patch)
tree8b041fb9c8630a51dfaf7f0c215562cb146137f3
parentMerge branch 'master' into develop (diff)
downloadgitbatch-80bf9c51de05e876f100bf5edf260f9cbb70cce2.tar.gz
initiated stash management feature
-rw-r--r--pkg/git/stash.go104
-rw-r--r--pkg/gui/keybindings.go18
-rw-r--r--pkg/gui/stashview.go53
3 files changed, 175 insertions, 0 deletions
diff --git a/pkg/git/stash.go b/pkg/git/stash.go
new file mode 100644
index 0000000..ea30276
--- /dev/null
+++ b/pkg/git/stash.go
@@ -0,0 +1,104 @@
+package git
+
+import (
+ "regexp"
+ "strings"
+ "strconv"
+
+ log "github.com/sirupsen/logrus"
+)
+
+var stashCommand = "stash"
+
+type StashedItem struct {
+ StashID int
+ BranchName string
+ Hash string
+ Description string
+}
+
+// // StashOption is the option argument for git stash command
+// type StashOption string
+
+// var (
+// StashPop StashOption = "pop"
+// StashPush StashOption = "push"
+// StashDrop StashOption = "drop"
+// )
+
+// // Stash used when you want to record the current state of the working
+// // directory and the index, but want to go back to a clean working directory.
+// func Stash(entity *RepoEntity, option StashOption) error {
+// args := make([]string, 0)
+// args = append(args, stashCommand)
+
+// if err := GenericGitCommand(entity.AbsPath, args); err != nil {
+// log.Warn("Error while stashing")
+// return err
+// }
+// return nil
+// }
+
+func stashGet(entity *RepoEntity, option string) string {
+ args := make([]string, 0)
+ args = append(args, stashCommand)
+ args = append(args, option)
+ out, err := GenericGitCommandWithOutput(entity.AbsPath, args)
+ if err != nil {
+ log.Warn("Error while stash command")
+ return "?"
+ }
+ return out
+}
+
+func (entity *RepoEntity) LoadStashedItems() ([]*StashedItem, error) {
+ stashedItems := make([]*StashedItem, 0)
+ output := stashGet(entity, "list")
+ stashIDRegex := regexp.MustCompile(`stash@{[\d]+}:`)
+ stashIDRegexInt := regexp.MustCompile(`[\d]+`)
+ stashBranchRegex := regexp.MustCompile(`[\w]+: `)
+ stashHashRegex := regexp.MustCompile(`[\w]{7}`)
+
+ stashlist := strings.Split(output, "\n")
+ for _, stashitem := range stashlist {
+ // find id
+ id := stashIDRegexInt.FindString(stashIDRegex.FindString(stashitem))
+ i, err := strconv.Atoi(id)
+ if err != nil {
+ // probably something isn't right let's continue over this iteration
+ log.Trace("cannot initiate stashed item")
+ continue
+ }
+ // trim id section
+ trimmed := stashIDRegex.Split(stashitem, 2)[1]
+
+ // find branch
+ stashBranchRegexMatch :=stashBranchRegex.FindString(trimmed)
+ branchName := stashBranchRegexMatch[:len(stashBranchRegexMatch)-2]
+
+ // trim branch section
+ trimmed = stashBranchRegex.Split(trimmed, 2)[1]
+ hash := stashHashRegex.FindString(trimmed)
+
+ // trim hash
+ trimmed = stashHashRegex.Split(trimmed, 2)[1][1:]
+
+ stashedItems = append(stashedItems, &StashedItem{
+ StashID: i,
+ BranchName: branchName,
+ Hash: hash,
+ Description: trimmed,
+ })
+ }
+ return stashedItems, nil
+}
+
+func (entity *RepoEntity) Stash() (error) {
+ args := make([]string, 0)
+ args = append(args, stashCommand)
+ if err := GenericGitCommand(entity.AbsPath, args); err != nil {
+ log.Warn("Error while stashing")
+ return err
+ }
+ return nil
+} \ No newline at end of file
diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go
index b2622bc..0f3f7b8 100644
--- a/pkg/gui/keybindings.go
+++ b/pkg/gui/keybindings.go
@@ -167,6 +167,14 @@ func (gui *Gui) generateKeybindings() error {
Description: "Sort repositories by Modification date",
Vital: false,
}, {
+ View: mainViewFeature.Name,
+ Key: 's',
+ Modifier: gocui.ModNone,
+ Handler: gui.showStashView,
+ Display: "s",
+ Description: "show stash",
+ Vital: true,
+ }, {
View: "",
Key: gocui.KeyCtrlC,
Modifier: gocui.ModNone,
@@ -412,6 +420,16 @@ func (gui *Gui) generateKeybindings() error {
Display: "c",
Description: "close/cancel",
Vital: true,
+ },
+ // Stash View
+ {
+ View: "stash",
+ Key: 'c',
+ Modifier: gocui.ModNone,
+ Handler: gui.closeStashView,
+ Display: "c",
+ Description: "close/cancel",
+ Vital: true,
},
}
for _, binding := range individualKeybindings {
diff --git a/pkg/gui/stashview.go b/pkg/gui/stashview.go
new file mode 100644
index 0000000..cbb4083
--- /dev/null
+++ b/pkg/gui/stashview.go
@@ -0,0 +1,53 @@
+package gui
+
+import (
+ "fmt"
+
+ "github.com/jroimartin/gocui"
+)
+
+var stashReturnView string
+
+func (gui *Gui) openStashView(g *gocui.Gui, returnViewName string) error {
+ maxX, maxY := g.Size()
+ stashReturnView = returnViewName
+ v, err := g.SetView("stash", maxX/2-30, maxY/2-3, maxX/2+30, maxY/2+3)
+ if err != nil {
+ if err != gocui.ErrUnknownView {
+ return err
+ }
+ v.Title = " Stashed Items "
+ v.Wrap = true
+ entity := gui.getSelectedRepository()
+ stashedItems, err := entity.LoadStashedItems()
+ if err != nil {
+ return err
+ }
+ for _, stashedItem := range stashedItems {
+ fmt.Fprintln(v, stashedItem)
+ }
+
+ }
+ gui.updateKeyBindingsView(g, "stash")
+ if _, err := g.SetCurrentView("stash"); err != nil {
+ return err
+ }
+ return nil
+}
+
+func (gui *Gui) closeStashView(g *gocui.Gui, v *gocui.View) error {
+
+ if err := g.DeleteView(v.Name()); err != nil {
+ return nil
+ }
+ if _, err := g.SetCurrentView(stashReturnView); err != nil {
+ return err
+ }
+ gui.updateKeyBindingsView(g, stashReturnView)
+ return nil
+}
+
+func (gui *Gui) showStashView(g *gocui.Gui, v *gocui.View) (err error) {
+ gui.openStashView(g, mainViewFeature.Name)
+ return nil
+}