mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-04-27 12:32:37 +02:00
More stuff
This commit is contained in:
parent
787e5d13ed
commit
ec78c795dd
@ -19,13 +19,20 @@ import (
|
|||||||
|
|
||||||
func handleBranchPress(g *gocui.Gui, v *gocui.View) error {
|
func handleBranchPress(g *gocui.Gui, v *gocui.View) error {
|
||||||
branch := getSelectedBranch(v)
|
branch := getSelectedBranch(v)
|
||||||
if err := gitCheckout(branch.Name, false); err != nil {
|
if output, err := gitCheckout(branch.Name, false); err != nil {
|
||||||
panic(err)
|
createSimpleConfirmationPanel(g, v, "Error", output)
|
||||||
}
|
}
|
||||||
refreshBranches(v)
|
return refreshSidePanels(g, v)
|
||||||
refreshFiles(g)
|
}
|
||||||
refreshLogs(g)
|
|
||||||
return nil
|
func handleForceCheckout(g *gocui.Gui, v *gocui.View) error {
|
||||||
|
branch := getSelectedBranch(v)
|
||||||
|
return createConfirmationPanel(g, v, "Force Checkout Branch", "Are you sure you want force checkout? You will lose all local changes (y/n)", func(g *gocui.Gui, v *gocui.View) error {
|
||||||
|
if output, err := gitCheckout(branch.Name, true); err != nil {
|
||||||
|
createSimpleConfirmationPanel(g, v, "Error", output)
|
||||||
|
}
|
||||||
|
return refreshSidePanels(g, v)
|
||||||
|
}, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSelectedBranch(v *gocui.View) Branch {
|
func getSelectedBranch(v *gocui.View) Branch {
|
||||||
@ -34,7 +41,7 @@ func getSelectedBranch(v *gocui.View) Branch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleBranchSelect(g *gocui.Gui, v *gocui.View) error {
|
func handleBranchSelect(g *gocui.Gui, v *gocui.View) error {
|
||||||
renderString(g, "options", "space: checkout")
|
renderString(g, "options", "space: checkout, s: squash down")
|
||||||
lineNumber := getItemPosition(v)
|
lineNumber := getItemPosition(v)
|
||||||
branch := state.Branches[lineNumber]
|
branch := state.Branches[lineNumber]
|
||||||
diff, _ := getBranchDiff(branch.Name, branch.BaseBranch)
|
diff, _ := getBranchDiff(branch.Name, branch.BaseBranch)
|
||||||
@ -44,7 +51,11 @@ func handleBranchSelect(g *gocui.Gui, v *gocui.View) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func refreshBranches(v *gocui.View) error {
|
func refreshBranches(g *gocui.Gui) error {
|
||||||
|
v, err := g.View("branches")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
state.Branches = getGitBranches()
|
state.Branches = getGitBranches()
|
||||||
yellow := color.New(color.FgYellow)
|
yellow := color.New(color.FgYellow)
|
||||||
red := color.New(color.FgRed)
|
red := color.New(color.FgRed)
|
||||||
|
@ -15,7 +15,7 @@ import (
|
|||||||
func handleCommitPress(g *gocui.Gui, currentView *gocui.View) error {
|
func handleCommitPress(g *gocui.Gui, currentView *gocui.View) error {
|
||||||
devLog(stagedFiles(state.GitFiles))
|
devLog(stagedFiles(state.GitFiles))
|
||||||
if len(stagedFiles(state.GitFiles)) == 0 {
|
if len(stagedFiles(state.GitFiles)) == 0 {
|
||||||
return createConfirmationPanel(g, currentView, "Nothing to Commit", "There are no staged files to commit (enter)", nil, nil)
|
return createSimpleConfirmationPanel(g, currentView, "Nothing to Commit", "There are no staged files to commit (esc)")
|
||||||
}
|
}
|
||||||
maxX, maxY := g.Size()
|
maxX, maxY := g.Size()
|
||||||
if v, err := g.SetView("commit", maxX/2-30, maxY/2-1, maxX/2+30, maxY/2+1); err != nil {
|
if v, err := g.SetView("commit", maxX/2-30, maxY/2-1, maxX/2+30, maxY/2+1); err != nil {
|
||||||
@ -44,7 +44,7 @@ func handleCommitSubmit(g *gocui.Gui, v *gocui.View) error {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
refreshFiles(g)
|
refreshFiles(g)
|
||||||
refreshLogs(g)
|
refreshCommits(g)
|
||||||
return closeCommitPrompt(g, v)
|
return closeCommitPrompt(g, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
79
commits_panel.go
Normal file
79
commits_panel.go
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
// lots of this has been directly ported from one of the example files, will brush up later
|
||||||
|
|
||||||
|
// Copyright 2014 The gocui Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/fatih/color"
|
||||||
|
"github.com/jroimartin/gocui"
|
||||||
|
)
|
||||||
|
|
||||||
|
func refreshCommits(g *gocui.Gui) error {
|
||||||
|
state.Commits = getCommits()
|
||||||
|
g.Update(func(*gocui.Gui) error {
|
||||||
|
v, err := g.View("commits")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
v.Clear()
|
||||||
|
yellow := color.New(color.FgYellow)
|
||||||
|
white := color.New(color.FgWhite)
|
||||||
|
for _, commit := range state.Commits {
|
||||||
|
yellow.Fprint(v, commit.Sha+" ")
|
||||||
|
white.Fprintln(v, commit.Name)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleCommitSelect(g *gocui.Gui, v *gocui.View) error {
|
||||||
|
commit := getSelectedCommit(v)
|
||||||
|
commitText := gitShow(commit.Sha)
|
||||||
|
devLog("commitText:", commitText)
|
||||||
|
return renderString(g, "main", commitText)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleCommitSquashDown(g *gocui.Gui, v *gocui.View) error {
|
||||||
|
if getItemPosition(v) != 0 {
|
||||||
|
return createSimpleConfirmationPanel(g, v, "Error", "Can only squash topmost commit")
|
||||||
|
}
|
||||||
|
commit := getSelectedCommit(v)
|
||||||
|
if output, err := gitSquashPreviousTwoCommits(commit.Name); err != nil {
|
||||||
|
return createSimpleConfirmationPanel(g, v, "Error", output)
|
||||||
|
}
|
||||||
|
if err := refreshCommits(g); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return handleCommitSelect(g, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleRenameCommit(g *gocui.Gui, v *gocui.View) error {
|
||||||
|
if getItemPosition(v) != 0 {
|
||||||
|
return createSimpleConfirmationPanel(g, v, "Error", "Can only rename topmost commit")
|
||||||
|
}
|
||||||
|
createPromptPanel(g, v, "Rename Commit", func(g *gocui.Gui, v *gocui.View) error {
|
||||||
|
if output, err := gitRenameCommit(v.Buffer()); err != nil {
|
||||||
|
return createSimpleConfirmationPanel(g, v, "Error", output)
|
||||||
|
}
|
||||||
|
if err := refreshCommits(g); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return handleCommitSelect(g, v)
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getSelectedCommit(v *gocui.View) Commit {
|
||||||
|
lineNumber := getItemPosition(v)
|
||||||
|
if len(state.Commits) == 0 {
|
||||||
|
return Commit{
|
||||||
|
Sha: "noCommit",
|
||||||
|
DisplayString: "none",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return state.Commits[lineNumber]
|
||||||
|
}
|
@ -11,7 +11,7 @@ import (
|
|||||||
// "io"
|
// "io"
|
||||||
// "io/ioutil"
|
// "io/ioutil"
|
||||||
|
|
||||||
"math"
|
"strings"
|
||||||
// "strings"
|
// "strings"
|
||||||
|
|
||||||
"github.com/jroimartin/gocui"
|
"github.com/jroimartin/gocui"
|
||||||
@ -24,39 +24,94 @@ func wrappedConfirmationFunction(function func(*gocui.Gui, *gocui.View) error) f
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := returnFocus(g, v); err != nil {
|
return closeConfirmationPrompt(g)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
g.DeleteKeybindings("confirmation")
|
|
||||||
return g.DeleteView("confirmation")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func closeConfirmationPrompt(g *gocui.Gui) error {
|
||||||
|
view, err := g.View("confirmation")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if err := returnFocus(g, view); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
g.DeleteKeybindings("confirmation")
|
||||||
|
return g.DeleteView("confirmation")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getMessageHeight(message string, width int) int {
|
||||||
|
lines := strings.Split(message, "\n")
|
||||||
|
lineCount := 0
|
||||||
|
for _, line := range lines {
|
||||||
|
lineCount += len(line)/width + 1
|
||||||
|
}
|
||||||
|
return lineCount
|
||||||
|
}
|
||||||
|
|
||||||
func getConfirmationPanelDimensions(g *gocui.Gui, prompt string) (int, int, int, int) {
|
func getConfirmationPanelDimensions(g *gocui.Gui, prompt string) (int, int, int, int) {
|
||||||
width, height := g.Size()
|
width, height := g.Size()
|
||||||
panelWidth := 60
|
panelWidth := 60
|
||||||
panelHeight := int(math.Ceil(float64(len(prompt)) / float64(panelWidth)))
|
// panelHeight := int(math.Ceil(float64(len(prompt)) / float64(panelWidth)))
|
||||||
|
panelHeight := getMessageHeight(prompt, panelWidth)
|
||||||
return width/2 - panelWidth/2,
|
return width/2 - panelWidth/2,
|
||||||
height/2 - panelHeight/2 - panelHeight%2 - 1,
|
height/2 - panelHeight/2 - panelHeight%2 - 1,
|
||||||
width/2 + panelWidth/2,
|
width/2 + panelWidth/2,
|
||||||
height/2 + panelHeight/2
|
height/2 + panelHeight/2
|
||||||
}
|
}
|
||||||
|
|
||||||
func createConfirmationPanel(g *gocui.Gui, sourceView *gocui.View, title, prompt string, handleYes, handleNo func(*gocui.Gui, *gocui.View) error) error {
|
func createPromptPanel(g *gocui.Gui, v *gocui.View, title string, handleSubmit func(*gocui.Gui, *gocui.View) error) error {
|
||||||
x0, y0, x1, y1 := getConfirmationPanelDimensions(g, prompt)
|
// only need to fit one line
|
||||||
if v, err := g.SetView("confirmation", x0, y0, x1, y1); err != nil {
|
x0, y0, x1, y1 := getConfirmationPanelDimensions(g, "")
|
||||||
|
if confirmationView, err := g.SetView("confirmation", x0, y0, x1, y1); err != nil {
|
||||||
if err != gocui.ErrUnknownView {
|
if err != gocui.ErrUnknownView {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
v.Title = title
|
confirmationView.Editable = true
|
||||||
renderString(g, "confirmation", prompt+" (y/n)")
|
g.Cursor = true
|
||||||
switchFocus(g, sourceView, v)
|
confirmationView.Title = title
|
||||||
if err := g.SetKeybinding("confirmation", 'n', gocui.ModNone, wrappedConfirmationFunction(handleNo)); err != nil {
|
switchFocus(g, v, confirmationView)
|
||||||
|
if err := g.SetKeybinding("confirmation", gocui.KeyEnter, gocui.ModNone, wrappedConfirmationFunction(handleSubmit)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := g.SetKeybinding("confirmation", 'y', gocui.ModNone, wrappedConfirmationFunction(handleYes)); err != nil {
|
if err := g.SetKeybinding("confirmation", gocui.KeyEsc, gocui.ModNone, wrappedConfirmationFunction(nil)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createConfirmationPanel(g *gocui.Gui, v *gocui.View, title, prompt string, handleYes, handleNo func(*gocui.Gui, *gocui.View) error) error {
|
||||||
|
// delete the existing confirmation panel if it exists
|
||||||
|
if view, _ := g.View("confirmation"); view != nil {
|
||||||
|
if err := closeConfirmationPrompt(g); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
x0, y0, x1, y1 := getConfirmationPanelDimensions(g, prompt)
|
||||||
|
if confirmationView, err := g.SetView("confirmation", x0, y0, x1, y1); err != nil {
|
||||||
|
if err != gocui.ErrUnknownView {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
confirmationView.Title = title
|
||||||
|
renderString(g, "confirmation", prompt)
|
||||||
|
switchFocus(g, v, confirmationView)
|
||||||
|
if err := g.SetKeybinding("confirmation", 'n', gocui.ModNone, wrappedConfirmationFunction(handleNo)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := g.SetKeybinding("confirmation", gocui.KeyEsc, gocui.ModNone, wrappedConfirmationFunction(handleNo)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := g.SetKeybinding("confirmation", 'y', gocui.ModNone, wrappedConfirmationFunction(handleYes)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := g.SetKeybinding("confirmation", gocui.KeyEnter, gocui.ModNone, wrappedConfirmationFunction(handleYes)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func createSimpleConfirmationPanel(g *gocui.Gui, v *gocui.View, title, prompt string) error {
|
||||||
|
return createConfirmationPanel(g, v, title, prompt, nil, nil)
|
||||||
|
}
|
||||||
|
@ -51,6 +51,7 @@ func handleFilePress(g *gocui.Gui, v *gocui.View) error {
|
|||||||
func getSelectedFile(v *gocui.View) GitFile {
|
func getSelectedFile(v *gocui.View) GitFile {
|
||||||
lineNumber := getItemPosition(v)
|
lineNumber := getItemPosition(v)
|
||||||
if len(state.GitFiles) == 0 {
|
if len(state.GitFiles) == 0 {
|
||||||
|
// find a way to not have to do this
|
||||||
return GitFile{
|
return GitFile{
|
||||||
Name: "noFile",
|
Name: "noFile",
|
||||||
DisplayString: "none",
|
DisplayString: "none",
|
||||||
@ -71,7 +72,7 @@ func handleFileRemove(g *gocui.Gui, v *gocui.View) error {
|
|||||||
} else {
|
} else {
|
||||||
deleteVerb = "delete"
|
deleteVerb = "delete"
|
||||||
}
|
}
|
||||||
return createConfirmationPanel(g, v, strings.Title(deleteVerb)+" file", "Are you sure you want to "+deleteVerb+" "+file.Name+" (you will lose your changes)?", func(g *gocui.Gui, v *gocui.View) error {
|
return createConfirmationPanel(g, v, strings.Title(deleteVerb)+" file", "Are you sure you want to "+deleteVerb+" "+file.Name+" (you will lose your changes)? (y/n)", func(g *gocui.Gui, v *gocui.View) error {
|
||||||
if err := removeFile(file); err != nil {
|
if err := removeFile(file); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -135,10 +136,30 @@ func refreshFiles(g *gocui.Gui) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func pullFiles(g *gocui.Gui, v *gocui.Gui) error {
|
func pullFiles(g *gocui.Gui, v *gocui.View) error {
|
||||||
if err := gitPull(); err != nil {
|
devLog("pulling...")
|
||||||
// should show error
|
createSimpleConfirmationPanel(g, v, "", "Pulling...")
|
||||||
panic(err)
|
go func() {
|
||||||
}
|
if output, err := gitPull(); err != nil {
|
||||||
|
createSimpleConfirmationPanel(g, v, "Error", output)
|
||||||
|
} else {
|
||||||
|
closeConfirmationPrompt(g)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
devLog("pulled.")
|
||||||
return refreshFiles(g)
|
return refreshFiles(g)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func pushFiles(g *gocui.Gui, v *gocui.View) error {
|
||||||
|
devLog("pushing...")
|
||||||
|
createSimpleConfirmationPanel(g, v, "", "Pushing...")
|
||||||
|
go func() {
|
||||||
|
if output, err := gitPush(); err != nil {
|
||||||
|
createSimpleConfirmationPanel(g, v, "Error", output)
|
||||||
|
} else {
|
||||||
|
closeConfirmationPrompt(g)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
devLog("pushed.")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -11,6 +11,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GitFile : A staged/unstaged file
|
// GitFile : A staged/unstaged file
|
||||||
@ -31,20 +33,32 @@ type Branch struct {
|
|||||||
BaseBranch string
|
BaseBranch string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Commit : A git commit
|
||||||
|
type Commit struct {
|
||||||
|
Sha string
|
||||||
|
Name string
|
||||||
|
DisplayString string
|
||||||
|
}
|
||||||
|
|
||||||
func devLog(objects ...interface{}) {
|
func devLog(objects ...interface{}) {
|
||||||
localLog("/Users/jesseduffieldduffield/go/src/github.com/jesseduffield/gitgot/development.log", objects...)
|
localLog(color.FgWhite, "/Users/jesseduffieldduffield/go/src/github.com/jesseduffield/gitgot/development.log", objects...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func colorLog(colour color.Attribute, objects ...interface{}) {
|
||||||
|
localLog(colour, "/Users/jesseduffieldduffield/go/src/github.com/jesseduffield/gitgot/development.log", objects...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func commandLog(objects ...interface{}) {
|
func commandLog(objects ...interface{}) {
|
||||||
localLog("/Users/jesseduffieldduffield/go/src/github.com/jesseduffield/gitgot/commands.log", objects...)
|
localLog(color.FgWhite, "/Users/jesseduffieldduffield/go/src/github.com/jesseduffield/gitgot/commands.log", objects...)
|
||||||
localLog("/Users/jesseduffieldduffield/go/src/github.com/jesseduffield/gitgot/development.log", objects...)
|
localLog(color.FgWhite, "/Users/jesseduffieldduffield/go/src/github.com/jesseduffield/gitgot/development.log", objects...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func localLog(path string, objects ...interface{}) {
|
func localLog(colour color.Attribute, path string, objects ...interface{}) {
|
||||||
f, _ := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0644)
|
f, _ := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0644)
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
for _, object := range objects {
|
for _, object := range objects {
|
||||||
f.WriteString(fmt.Sprint(object) + "\n")
|
colorFunction := color.New(colour).SprintFunc()
|
||||||
|
f.WriteString(colorFunction(fmt.Sprint(object)) + "\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,7 +99,7 @@ func mergeGitStatusFiles(oldGitFiles, newGitFiles []GitFile) []GitFile {
|
|||||||
|
|
||||||
func runDirectCommand(command string) (string, error) {
|
func runDirectCommand(command string) (string, error) {
|
||||||
commandLog(command)
|
commandLog(command)
|
||||||
cmdOut, err := exec.Command("bash", "-c", command).Output()
|
cmdOut, err := exec.Command("bash", "-c", command).CombinedOutput()
|
||||||
devLog(string(cmdOut))
|
devLog(string(cmdOut))
|
||||||
devLog(err)
|
devLog(err)
|
||||||
return string(cmdOut), err
|
return string(cmdOut), err
|
||||||
@ -120,7 +134,7 @@ func getGitBranches() []Branch {
|
|||||||
baseBranch = name
|
baseBranch = name
|
||||||
}
|
}
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
line = line[:2] + "\t*" + line[2:]
|
line = "* " + line
|
||||||
}
|
}
|
||||||
branches = append(branches, Branch{name, line, branchType, baseBranch})
|
branches = append(branches, Branch{name, line, branchType, baseBranch})
|
||||||
}
|
}
|
||||||
@ -156,19 +170,18 @@ func getGitStatusFiles() []GitFile {
|
|||||||
return gitFiles
|
return gitFiles
|
||||||
}
|
}
|
||||||
|
|
||||||
func gitCheckout(branch string, force bool) error {
|
func gitCheckout(branch string, force bool) (string, error) {
|
||||||
forceArg := ""
|
forceArg := ""
|
||||||
if force {
|
if force {
|
||||||
forceArg = "--force "
|
forceArg = "--force "
|
||||||
}
|
}
|
||||||
_, err := runCommand("git checkout " + forceArg + branch)
|
return runCommand("git checkout " + forceArg + branch)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func runCommand(cmd string) (string, error) {
|
func runCommand(command string) (string, error) {
|
||||||
commandLog(cmd)
|
commandLog(command)
|
||||||
splitCmd := strings.Split(cmd, " ")
|
splitCmd := strings.Split(command, " ")
|
||||||
cmdOut, err := exec.Command(splitCmd[0], splitCmd[1:]...).Output()
|
cmdOut, err := exec.Command(splitCmd[0], splitCmd[1:]...).CombinedOutput()
|
||||||
devLog(string(cmdOut[:]))
|
devLog(string(cmdOut[:]))
|
||||||
return string(cmdOut), err
|
return string(cmdOut), err
|
||||||
}
|
}
|
||||||
@ -185,8 +198,30 @@ func getBranchDiff(branch string, baseBranch string) (string, error) {
|
|||||||
return runCommand("git diff --color " + baseBranch + "..." + branch)
|
return runCommand("git diff --color " + baseBranch + "..." + branch)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getCommits() []Commit {
|
||||||
|
log := getLog()
|
||||||
|
commits := make([]Commit, 0)
|
||||||
|
// now we can split it up and turn it into commits
|
||||||
|
lines := splitLines(log)
|
||||||
|
for _, line := range lines {
|
||||||
|
splitLine := strings.Split(line, " ")
|
||||||
|
commits = append(commits, Commit{splitLine[0], strings.Join(splitLine[1:], " "), strings.Join(splitLine, " ")})
|
||||||
|
}
|
||||||
|
devLog(commits)
|
||||||
|
return commits
|
||||||
|
}
|
||||||
|
|
||||||
func getLog() string {
|
func getLog() string {
|
||||||
result, err := runDirectCommand("git log --color --oneline")
|
result, err := runDirectCommand("git log --oneline")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func gitShow(sha string) string {
|
||||||
|
result, err := runDirectCommand("git show --color " + sha)
|
||||||
|
// result, err := runDirectCommand("git show --color 10fd353")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -245,9 +280,34 @@ func gitCommit(message string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func gitPull() error {
|
func gitPull() (string, error) {
|
||||||
_, err := runDirectCommand("git pull --no-edit")
|
return runDirectCommand("git pull --no-edit")
|
||||||
return err
|
}
|
||||||
|
|
||||||
|
func gitPush() (string, error) {
|
||||||
|
return runDirectCommand("git push -u")
|
||||||
|
}
|
||||||
|
|
||||||
|
func gitSquashPreviousTwoCommits(message string) (string, error) {
|
||||||
|
return runDirectCommand("git reset --soft head^ && git commit --amend -m \"" + message + "\"")
|
||||||
|
}
|
||||||
|
|
||||||
|
func gitRenameCommit(message string) (string, error) {
|
||||||
|
return runDirectCommand("git commit --allow-empty --amend -m \"" + message + "\"")
|
||||||
|
}
|
||||||
|
|
||||||
|
func betterHaveWorked(err error) {
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func gitUpstreamDifferenceCount() (string, string) {
|
||||||
|
pushableCount, err := runDirectCommand("git rev-list @{u}..head --count")
|
||||||
|
betterHaveWorked(err)
|
||||||
|
pullableCount, err := runDirectCommand("git rev-list head..@{u} --count")
|
||||||
|
betterHaveWorked(err)
|
||||||
|
return pullableCount, pushableCount
|
||||||
}
|
}
|
||||||
|
|
||||||
const getBranchesCommand = `set -e
|
const getBranchesCommand = `set -e
|
||||||
@ -263,7 +323,7 @@ git reflog -n100 --pretty='%cr|%gs' --grep-reflog='checkout: moving' HEAD | {
|
|||||||
printf "%s\t%s\n" "$date" "$branch"
|
printf "%s\t%s\n" "$date" "$branch"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
done | sed 's/ days /d /g' | sed 's/ weeks /w /g' | sed 's/ hours /h /g' | sed 's/ minutes /m /g' | sed 's/ago//g' | tr -d ' '
|
done | sed 's/ days /d /g' | sed 's/ weeks /w /g' | sed 's/ hours /h /g' | sed 's/ minutes /m /g' | sed 's/ seconds /m /g' | sed 's/ago//g' | tr -d ' '
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
50
gui.go
50
gui.go
@ -20,15 +20,24 @@ import (
|
|||||||
type stateType struct {
|
type stateType struct {
|
||||||
GitFiles []GitFile
|
GitFiles []GitFile
|
||||||
Branches []Branch
|
Branches []Branch
|
||||||
|
Commits []Commit
|
||||||
PreviousView string
|
PreviousView string
|
||||||
}
|
}
|
||||||
|
|
||||||
var state = stateType{
|
var state = stateType{
|
||||||
GitFiles: make([]GitFile, 0),
|
GitFiles: make([]GitFile, 0),
|
||||||
PreviousView: "files",
|
PreviousView: "files",
|
||||||
|
Commits: make([]Commit, 0),
|
||||||
}
|
}
|
||||||
|
|
||||||
var cyclableViews = []string{"files", "branches"}
|
var cyclableViews = []string{"files", "branches", "commits"}
|
||||||
|
|
||||||
|
func refreshSidePanels(g *gocui.Gui, v *gocui.View) error {
|
||||||
|
refreshBranches(g)
|
||||||
|
refreshFiles(g)
|
||||||
|
refreshCommits(g)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func nextView(g *gocui.Gui, v *gocui.View) error {
|
func nextView(g *gocui.Gui, v *gocui.View) error {
|
||||||
var focusedViewName string
|
var focusedViewName string
|
||||||
@ -41,7 +50,8 @@ func nextView(g *gocui.Gui, v *gocui.View) error {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
if i == len(cyclableViews)-1 {
|
if i == len(cyclableViews)-1 {
|
||||||
panic(v.Name() + " is not in the list of views")
|
devLog(v.Name() + " is not in the list of views")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -68,6 +78,8 @@ func newLineFocused(g *gocui.Gui, v *gocui.View) error {
|
|||||||
return nil
|
return nil
|
||||||
case "main":
|
case "main":
|
||||||
return nil
|
return nil
|
||||||
|
case "commits":
|
||||||
|
return handleCommitSelect(g, v)
|
||||||
default:
|
default:
|
||||||
panic("No view matching newLineFocused switch statement")
|
panic("No view matching newLineFocused switch statement")
|
||||||
}
|
}
|
||||||
@ -113,19 +125,25 @@ func keybindings(g *gocui.Gui) error {
|
|||||||
if err := g.SetKeybinding("", gocui.KeyPgdn, gocui.ModNone, scrollDownMain); err != nil {
|
if err := g.SetKeybinding("", gocui.KeyPgdn, gocui.ModNone, scrollDownMain); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := g.SetKeybinding("", 'ç', gocui.ModNone, handleCommitPress); err != nil {
|
if err := g.SetKeybinding("files", 'c', gocui.ModNone, handleCommitPress); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := g.SetKeybinding("files", gocui.KeySpace, gocui.ModNone, handleFilePress); err != nil {
|
if err := g.SetKeybinding("files", gocui.KeySpace, gocui.ModNone, handleFilePress); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := g.SetKeybinding("files", '®', gocui.ModNone, handleFileRemove); err != nil {
|
if err := g.SetKeybinding("files", 'r', gocui.ModNone, handleFileRemove); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := g.SetKeybinding("files", 'ø', gocui.ModNone, handleFileOpen); err != nil {
|
if err := g.SetKeybinding("files", 'o', gocui.ModNone, handleFileOpen); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := g.SetKeybinding("files", 'ß', gocui.ModNone, handleSublimeFileOpen); err != nil {
|
if err := g.SetKeybinding("files", 's', gocui.ModNone, handleSublimeFileOpen); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := g.SetKeybinding("files", 'p', gocui.ModNone, pullFiles); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := g.SetKeybinding("files", 'P', gocui.ModNone, pushFiles); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := g.SetKeybinding("commit", gocui.KeyEsc, gocui.ModNone, closeCommitPrompt); err != nil {
|
if err := g.SetKeybinding("commit", gocui.KeyEsc, gocui.ModNone, closeCommitPrompt); err != nil {
|
||||||
@ -137,6 +155,15 @@ func keybindings(g *gocui.Gui) error {
|
|||||||
if err := g.SetKeybinding("branches", gocui.KeySpace, gocui.ModNone, handleBranchPress); err != nil {
|
if err := g.SetKeybinding("branches", gocui.KeySpace, gocui.ModNone, handleBranchPress); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if err := g.SetKeybinding("branches", 'F', gocui.ModNone, handleForceCheckout); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := g.SetKeybinding("commits", 's', gocui.ModNone, handleCommitSquashDown); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := g.SetKeybinding("commits", 'r', gocui.ModNone, handleRenameCommit); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if err := g.SetKeybinding("", '∑', gocui.ModNone, handleLogState); err != nil {
|
if err := g.SetKeybinding("", '∑', gocui.ModNone, handleLogState); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -146,6 +173,7 @@ func keybindings(g *gocui.Gui) error {
|
|||||||
func handleLogState(g *gocui.Gui, v *gocui.View) error {
|
func handleLogState(g *gocui.Gui, v *gocui.View) error {
|
||||||
devLog("state is:", state)
|
devLog("state is:", state)
|
||||||
devLog("previous view:", state.PreviousView)
|
devLog("previous view:", state.PreviousView)
|
||||||
|
refreshBranches(g)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,7 +199,7 @@ func layout(g *gocui.Gui) error {
|
|||||||
refreshFiles(g)
|
refreshFiles(g)
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, err := g.SetView("main", leftSideWidth+2, 0, width-1, optionsTop-1); err != nil {
|
if v, err := g.SetView("main", leftSideWidth+1, 0, width-1, optionsTop-1); err != nil {
|
||||||
if err != gocui.ErrUnknownView {
|
if err != gocui.ErrUnknownView {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -181,14 +209,14 @@ func layout(g *gocui.Gui) error {
|
|||||||
handleFileSelect(g, sideView)
|
handleFileSelect(g, sideView)
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, err := g.SetView("logs", 0, logsBranchesBoundary, leftSideWidth, optionsTop-1); err != nil {
|
if v, err := g.SetView("commits", 0, logsBranchesBoundary, leftSideWidth, optionsTop-1); err != nil {
|
||||||
if err != gocui.ErrUnknownView {
|
if err != gocui.ErrUnknownView {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
v.Title = "Log"
|
v.Title = "Commits"
|
||||||
|
|
||||||
// these are only called once
|
// these are only called once
|
||||||
refreshLogs(g)
|
refreshCommits(g)
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, err := g.SetView("branches", 0, filesBranchesBoundary, leftSideWidth, logsBranchesBoundary-1); err != nil {
|
if v, err := g.SetView("branches", 0, filesBranchesBoundary, leftSideWidth, logsBranchesBoundary-1); err != nil {
|
||||||
@ -198,7 +226,7 @@ func layout(g *gocui.Gui) error {
|
|||||||
v.Title = "Branches"
|
v.Title = "Branches"
|
||||||
|
|
||||||
// these are only called once
|
// these are only called once
|
||||||
refreshBranches(v)
|
refreshBranches(g)
|
||||||
nextView(g, nil)
|
nextView(g, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,30 +0,0 @@
|
|||||||
// lots of this has been directly ported from one of the example files, will brush up later
|
|
||||||
|
|
||||||
// Copyright 2014 The gocui Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/jroimartin/gocui"
|
|
||||||
)
|
|
||||||
|
|
||||||
func refreshLogs(g *gocui.Gui) error {
|
|
||||||
// here is where you want to pickup from
|
|
||||||
// state.Logs = getGitLogs(nil)
|
|
||||||
s := getLog()
|
|
||||||
g.Update(func(*gocui.Gui) error {
|
|
||||||
v, err := g.View("logs")
|
|
||||||
v.Clear()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
v.Clear()
|
|
||||||
fmt.Fprint(v, s)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
return nil
|
|
||||||
}
|
|
4
main.go
4
main.go
@ -1,6 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "github.com/fatih/color"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
a, b := gitUpstreamDifferenceCount()
|
||||||
|
colorLog(color.FgRed, a, b)
|
||||||
devLog("\n\n\n\n\n\n\n\n\n\n")
|
devLog("\n\n\n\n\n\n\n\n\n\n")
|
||||||
run()
|
run()
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ func switchFocus(g *gocui.Gui, oldView, newView *gocui.View) error {
|
|||||||
if _, err := g.SetCurrentView(newView.Name()); err != nil {
|
if _, err := g.SetCurrentView(newView.Name()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
g.Cursor = newView.Name() == "commit"
|
g.Cursor = newView.Editable
|
||||||
return newLineFocused(g, newView)
|
return newLineFocused(g, newView)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user