1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-08-06 11:02:41 +03:00
This commit is contained in:
Jesse Duffield
2018-08-23 18:43:16 +10:00
parent 954dfb12e4
commit 5628eae502
5 changed files with 180 additions and 63 deletions

View File

@@ -18,7 +18,7 @@ func (gui *Gui) wrappedConfirmationFunction(function func(*gocui.Gui, *gocui.Vie
return func(g *gocui.Gui, v *gocui.View) error {
if function != nil {
if err := function(g, v); err != nil {
panic(err)
return err
}
}
return gui.closeConfirmationPrompt(g)

View File

@@ -251,6 +251,14 @@ func (gui *Gui) handleFileEdit(g *gocui.Gui, v *gocui.View) error {
return gui.genericFileOpen(g, v, file.Name, gui.OSCommand.EditFile)
}
func (gui *Gui) openFile(filename string) error {
err := gui.OSCommand.OpenFile(filename)
if err != nil {
return gui.createErrorPanel(gui.g, err.Error())
}
return nil
}
func (gui *Gui) handleFileOpen(g *gocui.Gui, v *gocui.View) error {
file, err := gui.getSelectedFile(g)
if err != nil {

View File

@@ -80,6 +80,7 @@ type guiState struct {
Conflicts []commands.Conflict
EditHistory *stack.Stack
Platform commands.Platform
Updating bool
}
// NewGui builds a new gui handler
@@ -140,6 +141,13 @@ func max(a, b int) int {
return b
}
func (gui *Gui) setAppStatus(status string) error {
if err := gui.renderString(gui.g, "appStatus", status); err != nil {
return err
}
return nil
}
// layout is called for every screen re-render e.g. when the screen is resized
func (gui *Gui) layout(g *gocui.Gui) error {
g.Highlight = true
@@ -153,6 +161,12 @@ func (gui *Gui) layout(g *gocui.Gui) error {
minimumWidth := 10
version := gui.Config.GetVersion()
appStatusView, _ := g.View("appStatus")
appStatusOptionsBoundary := -2
if appStatusView != nil && len(appStatusView.Buffer()) > 2 {
appStatusOptionsBoundary = len(appStatusView.Buffer()) + 2
}
panelSpacing := 1
if OverlappingEdges {
panelSpacing = 0
@@ -230,7 +244,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
v.FgColor = gocui.ColorWhite
}
if v, err := g.SetView("options", -1, optionsTop, width-len(version)-2, optionsTop+2, 0); err != nil {
if v, err := g.SetView("options", appStatusOptionsBoundary-1, optionsTop, width-len(version)-2, optionsTop+2, 0); err != nil {
if err != gocui.ErrUnknownView {
return err
}
@@ -253,6 +267,20 @@ func (gui *Gui) layout(g *gocui.Gui) error {
}
}
appStatusLeft := -1
if appStatusOptionsBoundary < 2 {
appStatusLeft = -5
}
if v, err := g.SetView("appStatus", appStatusLeft, optionsTop, appStatusOptionsBoundary, optionsTop+2, 0); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.BgColor = gocui.ColorDefault
v.FgColor = gocui.ColorCyan
v.Frame = false
}
if v, err := g.SetView("version", width-len(version)-1, optionsTop, width, optionsTop+2, 0); err != nil {
if err != gocui.ErrUnknownView {
return err
@@ -264,18 +292,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
return err
}
newVersion, err := gui.Updater.CheckForNewUpdate()
if err != nil {
return err
}
gui.Updater.NewVersion = "v0.1.75"
newVersion = "v0.1.75"
if newVersion != "" {
if err := gui.Updater.Update(); err != nil {
panic(err)
return err
}
}
// gui.Updater.CheckForNewUpdate(gui.onUpdateCheckFinish)
// these are only called once
gui.handleFileSelect(g, filesView)
@@ -293,6 +310,36 @@ func (gui *Gui) layout(g *gocui.Gui) error {
return nil
}
func (gui *Gui) onUpdateFinish(err error) error {
gui.State.Updating = false
gui.setAppStatus("")
if err != nil {
gui.createErrorPanel(gui.g, "Update failed: "+err.Error())
}
// TODO: on attempted quit, if downloading is true, ask if sure the user wants to quit
return nil
}
func (gui *Gui) onUpdateCheckFinish(newVersion string, err error) error {
newVersion = "v0.1.72"
if err != nil {
// ignoring the error for now
return nil
}
if newVersion == "" {
return nil
}
title := "New version available!"
message := "Download latest version? (enter/esc)"
// TODO: support nil view in createConfirmationPanel or always go back to filesPanel when there is no previous view
return gui.createConfirmationPanel(gui.g, nil, title, message, func(g *gocui.Gui, v *gocui.View) error {
gui.State.Updating = true
gui.setAppStatus("updating...")
gui.Updater.Update(newVersion, gui.onUpdateFinish)
return nil
}, nil) // TODO: set config value saying not to check for another while if user hits escape
}
func (gui *Gui) fetch(g *gocui.Gui) error {
gui.GitCommand.Fetch()
gui.refreshStatus(g)
@@ -300,11 +347,17 @@ func (gui *Gui) fetch(g *gocui.Gui) error {
}
func (gui *Gui) updateLoader(g *gocui.Gui) error {
if confirmationView, _ := g.View("confirmation"); confirmationView != nil {
content := gui.trimmedContent(confirmationView)
if strings.Contains(content, "...") {
staticContent := strings.Split(content, "...")[0] + "..."
gui.renderString(g, "confirmation", staticContent+" "+gui.loader())
viewNames := []string{"confirmation", "appStatus"}
for _, viewName := range viewNames {
if view, _ := g.View(viewName); view != nil {
content := gui.trimmedContent(view)
if strings.Contains(content, "...") {
staticContent := strings.Split(content, "...")[0] + "..."
if viewName == "appStatus" {
// panic(staticContent + " " + gui.loader())
}
gui.renderString(g, viewName, staticContent+" "+gui.loader())
}
}
}
return nil
@@ -334,9 +387,6 @@ func (gui *Gui) Run() error {
}
defer g.Close()
// TODO: do this more elegantly
gui.Updater.CheckForNewUpdate()
gui.g = g // TODO: always use gui.g rather than passing g around everywhere
if err := gui.SetColorScheme(); err != nil {
@@ -381,6 +431,17 @@ func (gui *Gui) RunWithSubprocesses() {
}
}
func (gui *Gui) createUpdateQuitConfirmation(g *gocui.Gui, v *gocui.View) error {
title := "Currently Updating"
message := "An update is in progress. Are you sure you want to quit?"
return gui.createConfirmationPanel(gui.g, v, title, message, func(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}, nil)
}
func (gui *Gui) quit(g *gocui.Gui, v *gocui.View) error {
if gui.State.Updating {
return gui.createUpdateQuitConfirmation(g, v)
}
return gocui.ErrQuit
}

View File

@@ -108,7 +108,11 @@ func (gui *Gui) newLineFocused(g *gocui.Gui, v *gocui.View) error {
func (gui *Gui) returnFocus(g *gocui.Gui, v *gocui.View) error {
previousView, err := g.View(gui.State.PreviousView)
if err != nil {
panic(err)
// always fall back to files view if there's no 'previous' view stored
previousView, err = g.View("files")
if err != nil {
gui.Log.Error(err)
}
}
return gui.switchFocus(g, v, previousView)
}
@@ -216,6 +220,9 @@ func (gui *Gui) renderString(g *gocui.Gui, viewName, s string) error {
if err != nil {
return nil
}
if viewName == "appStatus" {
gui.Log.Info(s)
}
v.Clear()
fmt.Fprint(v, s)
v.Wrap = true

View File

@@ -2,7 +2,6 @@ package updates
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
@@ -10,11 +9,12 @@ import (
"os"
"path/filepath"
"runtime"
"time"
"github.com/kardianos/osext"
"github.com/Sirupsen/logrus"
getter "github.com/hashicorp/go-getter"
getter "github.com/jesseduffield/go-getter"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/config"
)
@@ -22,9 +22,8 @@ import (
// Update checks for updates and does updates
type Updater struct {
LastChecked string
Log *logrus.Logger
Log *logrus.Entry
Config config.AppConfigurer
NewVersion string
OSCommand *commands.OSCommand
}
@@ -40,10 +39,11 @@ var (
// NewUpdater creates a new updater
func NewUpdater(log *logrus.Logger, config config.AppConfigurer, osCommand *commands.OSCommand) (*Updater, error) {
contextLogger := log.WithField("context", "updates")
updater := &Updater{
LastChecked: "today",
Log: log,
Log: contextLogger,
Config: config,
OSCommand: osCommand,
}
@@ -51,6 +51,7 @@ func NewUpdater(log *logrus.Logger, config config.AppConfigurer, osCommand *comm
}
func (u *Updater) getLatestVersionNumber() (string, error) {
time.Sleep(5)
req, err := http.NewRequest("GET", projectUrl+"/releases/latest", nil)
if err != nil {
return "", err
@@ -75,27 +76,45 @@ func (u *Updater) getLatestVersionNumber() (string, error) {
return dat["tag_name"].(string), nil
}
// CheckForNewUpdate checks if there is an available update
func (u *Updater) CheckForNewUpdate() (string, error) {
func (u *Updater) checkForNewUpdate() (string, error) {
u.Log.Info("Checking for an updated version")
if u.Config.GetVersion() == "unversioned" {
u.Log.Info("Current version is not built from an official release so we won't check for an update")
return "", nil
}
// if u.Config.GetVersion() == "unversioned" {
// u.Log.Info("Current version is not built from an official release so we won't check for an update")
// return "", nil
// }
newVersion, err := u.getLatestVersionNumber()
if err != nil {
return "", err
}
u.NewVersion = newVersion
u.Log.Info("Current version is " + u.Config.GetVersion())
u.Log.Info("New version is " + newVersion)
if newVersion == u.Config.GetVersion() {
// if newVersion == u.Config.GetVersion() {
// return "", nil
// }
rawUrl, err := u.getBinaryUrl(newVersion)
if err != nil {
return "", err
}
u.Log.Info("Checking for resource at url " + rawUrl)
if !u.verifyResourceFound(rawUrl) {
u.Log.Error("Resource not found")
return "", nil
}
// TODO: verify here that there is a binary available for this OS/arch
u.Log.Info("Verified resource is available, ready to update")
return newVersion, nil
}
// CheckForNewUpdate checks if there is an available update
func (u *Updater) CheckForNewUpdate(onFinish func(string, error) error) {
go func() {
newVersion, err := u.checkForNewUpdate()
if err = onFinish(newVersion, err); err != nil {
u.Log.Error(err)
}
}()
}
func (u *Updater) mappedOs(os string) string {
osMap := map[string]string{
"darwin": "Darwin",
@@ -122,10 +141,7 @@ func (u *Updater) mappedArch(arch string) string {
}
// example: https://github.com/jesseduffield/lazygit/releases/download/v0.1.73/lazygit_0.1.73_Darwin_x86_64.tar.gz
func (u *Updater) getBinaryUrl() (string, error) {
if u.NewVersion == "" {
return "", errors.New("Must run CheckForUpdate() before running getBinaryUrl() to get the new version number")
}
func (u *Updater) getBinaryUrl(newVersion string) (string, error) {
extension := "tar.gz"
if runtime.GOOS == "windows" {
extension = "zip"
@@ -133,8 +149,8 @@ func (u *Updater) getBinaryUrl() (string, error) {
url := fmt.Sprintf(
"%s/releases/download/%s/lazygit_%s_%s_%s.%s",
projectUrl,
u.NewVersion,
u.NewVersion[1:],
newVersion,
newVersion[1:],
u.mappedOs(runtime.GOOS),
u.mappedArch(runtime.GOARCH),
extension,
@@ -143,54 +159,79 @@ func (u *Updater) getBinaryUrl() (string, error) {
return url, nil
}
func (u *Updater) Update() error {
rawUrl, err := u.getBinaryUrl()
// Update downloads the latest binary and replaces the current binary with it
func (u *Updater) Update(newVersion string, onFinish func(error) error) {
go func() {
err := u.update(newVersion)
if err = onFinish(err); err != nil {
u.Log.Error(err)
}
}()
}
func (u *Updater) update(newVersion string) error {
rawUrl, err := u.getBinaryUrl(newVersion)
if err != nil {
return err
}
u.Log.Info("updating with url " + rawUrl)
return u.downloadAndInstall(rawUrl)
}
func (u *Updater) downloadAndInstall(rawUrl string) error {
url, err := url.Parse(rawUrl)
if err != nil {
panic(err)
return err
}
g := new(getter.HttpGetter)
tempDir, err := ioutil.TempDir("", "lazygit")
if err != nil {
panic(err)
return err
}
defer os.RemoveAll(tempDir)
u.Log.Info("temp directory is " + tempDir)
// Get it!
if err := g.Get(tempDir, url); err != nil {
panic(err)
}
extension := ""
if runtime.GOOS == "windows" {
extension = ".exe"
}
// Verify the main file exists
tempPath := filepath.Join(tempDir, "lazygit"+extension)
if _, err := os.Stat(tempPath); err != nil {
panic(err)
return err
}
// get the path of the current binary
execPath, err := osext.Executable()
binaryPath, err := osext.Executable()
if err != nil {
panic(err)
return err
}
u.Log.Info("binary path is " + binaryPath)
binaryName := filepath.Base(binaryPath)
u.Log.Info("binary name is " + binaryName)
// Verify the main file exists
tempPath := filepath.Join(tempDir, binaryName)
u.Log.Info("temp path to binary is " + tempPath)
if _, err := os.Stat(tempPath); err != nil {
return err
}
// swap out the old binary for the new one
err = os.Rename(tempPath, execPath+"2")
err = os.Rename(tempPath, binaryPath)
if err != nil {
panic(err)
return err
}
u.Log.Info("update complete!")
return nil
}
func (u *Updater) verifyResourceFound(rawUrl string) bool {
resp, err := http.Head(rawUrl)
if err != nil {
return false
}
defer resp.Body.Close()
u.Log.Info("Received status code ", resp.StatusCode)
// 403 means the resource is there (not going to bother adding extra request headers)
// 404 means its not
return resp.StatusCode == 403
}