Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Apr 8, 2020
1 parent 9672370 commit e73eb8b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 225 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func main() {
log.Fatal(err.Error())
}

app, err := app.NewApp(appConfig, filterPath)
app, err := app.NewApp(appConfig)

if err == nil {
err = app.Run()
Expand Down
105 changes: 40 additions & 65 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package app

import (
"bufio"
"fmt"
"errors"
"io"
"io/ioutil"
"os"
Expand All @@ -22,14 +21,13 @@ import (
type App struct {
closers []io.Closer

Config config.AppConfigurer
Log *logrus.Entry
OSCommand *commands.OSCommand
GitCommand *commands.GitCommand
Gui *gui.Gui
Tr *i18n.Localizer
Updater *updates.Updater // may only need this on the Gui
ClientContext string
Config config.AppConfigurer
Log *logrus.Entry
OSCommand *commands.OSCommand
GitCommand *commands.GitCommand
Gui *gui.Gui
Tr *i18n.Localizer
Updater *updates.Updater // may only need this on the Gui
}

type errorMapping struct {
Expand Down Expand Up @@ -91,7 +89,7 @@ func newLogger(config config.AppConfigurer) *logrus.Entry {
}

// NewApp bootstrap a new application
func NewApp(config config.AppConfigurer, filterPath string) (*App, error) {
func NewApp(config config.AppConfigurer) (*App, error) {
app := &App{
closers: []io.Closer{},
Config: config,
Expand All @@ -100,84 +98,66 @@ func NewApp(config config.AppConfigurer, filterPath string) (*App, error) {
app.Log = newLogger(config)
app.Tr = i18n.NewLocalizer(app.Log)

// if we are being called in 'demon' mode, we can just return here
app.ClientContext = os.Getenv("LAZYGIT_CLIENT_COMMAND")
if app.ClientContext != "" {
return app, nil
}

app.OSCommand = commands.NewOSCommand(app.Log, config)

app.Updater, err = updates.NewUpdater(app.Log, config, app.OSCommand, app.Tr)
if err != nil {
return app, err
}

if err := app.setupRepo(); err != nil {
if err := app.setupPackage(); err != nil {
return app, err
}

app.GitCommand, err = commands.NewGitCommand(app.Log, app.OSCommand, app.Tr, app.Config)
if err != nil {
return app, err
}
app.Gui, err = gui.NewGui(app.Log, app.GitCommand, app.OSCommand, app.Tr, config, app.Updater, filterPath)
app.Gui, err = gui.NewGui(app.Log, app.GitCommand, app.OSCommand, app.Tr, config, app.Updater)
if err != nil {
return app, err
}
return app, nil
}

func (app *App) setupRepo() error {
// if we are not in a git repo, we ask if we want to `git init`
if err := app.OSCommand.RunCommand("git status"); err != nil {
if !strings.Contains(err.Error(), "Not a git repository") {
return err
}
fmt.Print(app.Tr.SLocalize("CreateRepo"))
response, _ := bufio.NewReader(os.Stdin).ReadString('\n')
if strings.Trim(response, " \n") != "y" {
os.Exit(1)
}
if err := app.OSCommand.RunCommand("git init"); err != nil {
return err
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return nil
return !info.IsDir()
}

func (app *App) Run() error {
if app.ClientContext == "INTERACTIVE_REBASE" {
return app.Rebase()
}

if app.ClientContext == "EXIT_IMMEDIATELY" {
os.Exit(0)
func (app *App) setupPackage() error {
// ensure we have a package.json file here or in an ancestor directory
// if there's not, pick the first one from state or return an error
dir, err := os.Getwd()
if err != nil {
return err
}
for {
if fileExists("package.json") {
return nil
}

err := app.Gui.RunWithSubprocesses()
return err
}

// Rebase contains logic for when we've been run in demon mode, meaning we've
// given lazynpm as a command for git to call e.g. to edit a file
func (app *App) Rebase() error {
app.Log.Info("Lazynpm invoked as interactive rebase demon")
app.Log.Info("args: ", os.Args)

if strings.HasSuffix(os.Args[1], "git-rebase-todo") {
if err := ioutil.WriteFile(os.Args[1], []byte(os.Getenv("LAZYGIT_REBASE_TODO")), 0644); err != nil {
if err := os.Chdir(".."); err != nil {
return err
}

} else if strings.HasSuffix(os.Args[1], ".git/COMMIT_EDITMSG") {
// if we are rebasing and squashing, we'll see a COMMIT_EDITMSG
// but in this case we don't need to edit it, so we'll just return
} else {
app.Log.Info("Lazynpm demon did not match on any use cases")
newDir, err := os.Getwd()
if err != nil {
return err
}
if newDir == dir {
return errors.New("must start lazynpm in an npm package")
}
dir = newDir
}
}

return nil
func (app *App) Run() error {
err := app.Gui.RunWithSubprocesses()
return err
}

// Close closes any resources
Expand All @@ -195,12 +175,7 @@ func (app *App) Close() error {
func (app *App) KnownError(err error) (string, bool) {
errorMessage := err.Error()

mappings := []errorMapping{
{
originalError: "fatal: not a git repository (or any of the parent directories): .git",
newError: app.Tr.SLocalize("notARepository"),
},
}
mappings := []errorMapping{}

for _, mapping := range mappings {
if strings.Contains(errorMessage, mapping.originalError) {
Expand Down
146 changes: 0 additions & 146 deletions pkg/gui/file_watching.go

This file was deleted.

4 changes: 0 additions & 4 deletions pkg/gui/files_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,6 @@ func (gui *Gui) refreshStateFiles() error {
files := gui.GitCommand.GetStatusFiles()
gui.State.Files = gui.GitCommand.MergeStatusFiles(gui.State.Files, files)

if err := gui.fileWatcher.addFilesToFileWatcher(files); err != nil {
return err
}

gui.refreshSelectedLine(&gui.State.Panels.Files.SelectedLine, len(gui.State.Files))
return nil
}
Expand Down
10 changes: 1 addition & 9 deletions pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ type Gui struct {
statusManager *statusManager
credentials credentials
waitForIntro sync.WaitGroup
fileWatcher *fileWatcher
viewBufferManagerMap map[string]*tasks.ViewBufferManager
stopChan chan struct{}
}
Expand Down Expand Up @@ -273,7 +272,7 @@ func (gui *Gui) resetState() {

// for now the split view will always be on
// NewGui builds a new gui handler
func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *commands.OSCommand, tr *i18n.Localizer, config config.AppConfigurer, updater *updates.Updater, filterPath string) (*Gui, error) {
func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *commands.OSCommand, tr *i18n.Localizer, config config.AppConfigurer, updater *updates.Updater) (*Gui, error) {
gui := &Gui{
Log: log,
GitCommand: gitCommand,
Expand All @@ -286,9 +285,6 @@ func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *comma
}

gui.resetState()
gui.State.FilterPath = filterPath

gui.watchFilesForChanges()

gui.GenerateSentinelErrors()

Expand Down Expand Up @@ -371,10 +367,6 @@ func (gui *Gui) RunWithSubprocesses() error {
}
gui.viewBufferManagerMap = map[string]*tasks.ViewBufferManager{}

if !gui.fileWatcher.Disabled {
gui.fileWatcher.Watcher.Close()
}

close(gui.stopChan)

if err == gocui.ErrQuit {
Expand Down

0 comments on commit e73eb8b

Please sign in to comment.