From e2f4b027c946831c3f4f119d87a80513c7cf8fdc Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 12 Aug 2018 21:09:30 +0200 Subject: termui: implement push/pull --- termui/bug_table.go | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-) (limited to 'termui/bug_table.go') diff --git a/termui/bug_table.go b/termui/bug_table.go index 1432d943..cd5ee1f6 100644 --- a/termui/bug_table.go +++ b/termui/bug_table.go @@ -1,7 +1,9 @@ package termui import ( + "bytes" "fmt" + "github.com/MichaelMure/git-bug/bug" "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/util" @@ -14,6 +16,8 @@ const bugTableHeaderView = "bugTableHeaderView" const bugTableFooterView = "bugTableFooterView" const bugTableInstructionView = "bugTableInstructionView" +const remote = "origin" + type bugTable struct { repo cache.RepoCacher allIds []string @@ -105,7 +109,7 @@ func (bt *bugTable) layout(g *gocui.Gui) error { v.Frame = false v.BgColor = gocui.ColorBlue - fmt.Fprintf(v, "[q] Quit [←,h] Previous page [↓,j] Down [↑,k] Up [→,l] Next page [enter] Open bug [n] New bug") + fmt.Fprintf(v, "[q] Quit [←↓↑→,hjkl] Navigation [enter] Open bug [n] New bug [i] Pull [o] Push") } _, err = g.SetCurrentView(bugTableView) @@ -176,6 +180,18 @@ func (bt *bugTable) keybindings(g *gocui.Gui) error { return err } + // Pull + if err := g.SetKeybinding(bugTableView, 'i', gocui.ModNone, + bt.pull); err != nil { + return err + } + + // Push + if err := g.SetKeybinding(bugTableView, 'o', gocui.ModNone, + bt.push); err != nil { + return err + } + return nil } @@ -383,3 +399,85 @@ func (bt *bugTable) openBug(g *gocui.Gui, v *gocui.View) error { ui.showBug.SetBug(bt.bugs[bt.pageCursor+y]) return ui.activateWindow(ui.showBug) } + +func (bt *bugTable) pull(g *gocui.Gui, v *gocui.View) error { + // Note: this is very hacky + + ui.msgPopup.Activate("Pull from remote "+remote, "...") + + go func() { + stdout, err := bt.repo.Fetch(remote) + + if err != nil { + g.Update(func(gui *gocui.Gui) error { + ui.msgPopup.Activate(msgPopupErrorTitle, err.Error()) + return nil + }) + } else { + g.Update(func(gui *gocui.Gui) error { + ui.msgPopup.UpdateMessage(stdout) + return nil + }) + } + + var buffer bytes.Buffer + beginLine := "" + + for merge := range bt.repo.MergeAll(remote) { + if merge.Status == bug.MsgMergeNothing { + continue + } + + if merge.Err != nil { + g.Update(func(gui *gocui.Gui) error { + ui.msgPopup.Activate(msgPopupErrorTitle, err.Error()) + return nil + }) + } else { + fmt.Fprintf(&buffer, "%s%s: %s", + beginLine, util.Cyan(merge.HumanId), merge.Status, + ) + + beginLine = "\n" + + g.Update(func(gui *gocui.Gui) error { + ui.msgPopup.UpdateMessage(buffer.String()) + return nil + }) + } + } + + fmt.Fprintf(&buffer, "%sdone", beginLine) + + g.Update(func(gui *gocui.Gui) error { + ui.msgPopup.UpdateMessage(buffer.String()) + return nil + }) + + }() + + return nil +} + +func (bt *bugTable) push(g *gocui.Gui, v *gocui.View) error { + ui.msgPopup.Activate("Push to remote "+remote, "...") + + go func() { + // TODO: make the remote configurable + stdout, err := bt.repo.Push(remote) + + if err != nil { + g.Update(func(gui *gocui.Gui) error { + ui.msgPopup.Activate(msgPopupErrorTitle, err.Error()) + return nil + }) + } else { + g.Update(func(gui *gocui.Gui) error { + ui.msgPopup.UpdateMessage(stdout) + return nil + }) + } + }() + + return nil +} -- cgit