Skip to content

Commit

Permalink
Merge branch 'master' into feature/anonymous-reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Aug 27, 2018
2 parents 540edc0 + e72d090 commit a1c6ada
Show file tree
Hide file tree
Showing 12 changed files with 408 additions and 98 deletions.
19 changes: 19 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,33 @@ jobs:
working_directory: /go/src/github.com/jesseduffield/lazygit
steps:
- checkout
- restore_cache:
keys:
- v1-pkg-cache
- run:
name: Run gofmt -s
command: |
if [ $(find . ! -path "./vendor/*" -name "*.go" -exec gofmt -s -d {} \;|wc -l) -gt 0 ]; then
find . ! -path "./vendor/*" -name "*.go" -exec gofmt -s -d {} \;
exit 1;
fi
- run:
name: Run tests
command: |
./test.sh
- run:
name: Compile project on every platform
command: |
go get github.com/mitchellh/gox
gox -parallel 10 -os "linux freebsd netbsd windows" -osarch "darwin/i386 darwin/amd64"
- run:
name: Push on codecov result
command: |
bash <(curl -s https://codecov.io/bash)
- save_cache:
key: v1-pkg-cache
paths:
- "/go/pkg"

release:
docker:
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
version = "unversioned"
date string

configFlag = flag.Bool("config", false, "Print the current default config")
debuggingFlag = flag.Bool("debug", false, "a boolean")
versionFlag = flag.Bool("v", false, "Print the current version")
)
Expand All @@ -31,6 +32,10 @@ func main() {
fmt.Printf("commit=%s, build date=%s, version=%s, os=%s, arch=%s\n", commit, date, version, runtime.GOOS, runtime.GOARCH)
os.Exit(0)
}
if *configFlag {
fmt.Printf("%s\n", config.GetDefaultConfig())
os.Exit(0)
}
appConfig, err := config.NewAppConfig("lazygit", version, commit, date, debuggingFlag)
if err != nil {
panic(err)
Expand Down
5 changes: 1 addition & 4 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ func NewApp(config config.AppConfigurer) (*App, error) {
}
var err error
app.Log = newLogger(config)
app.OSCommand, err = commands.NewOSCommand(app.Log)
if err != nil {
return app, err
}
app.OSCommand = commands.NewOSCommand(app.Log)

app.Tr = i18n.NewLocalizer(app.Log)

Expand Down
6 changes: 3 additions & 3 deletions pkg/commands/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func (c *GitCommand) UsingGpg() bool {
func (c *GitCommand) Commit(g *gocui.Gui, message string) (*exec.Cmd, error) {
command := "git commit -m " + c.OSCommand.Quote(message)
if c.UsingGpg() {
return c.OSCommand.PrepareSubProcess(c.OSCommand.Platform.shell, c.OSCommand.Platform.shellArg, command)
return c.OSCommand.PrepareSubProcess(c.OSCommand.Platform.shell, c.OSCommand.Platform.shellArg, command), nil
}
return nil, c.OSCommand.RunCommand(command)
}
Expand Down Expand Up @@ -391,12 +391,12 @@ func (c *GitCommand) Checkout(branch string, force bool) error {

// AddPatch prepares a subprocess for adding a patch by patch
// this will eventually be swapped out for a better solution inside the Gui
func (c *GitCommand) AddPatch(filename string) (*exec.Cmd, error) {
func (c *GitCommand) AddPatch(filename string) *exec.Cmd {
return c.OSCommand.PrepareSubProcess("git", "add", "--patch", filename)
}

// PrepareCommitSubProcess prepares a subprocess for `git commit`
func (c *GitCommand) PrepareCommitSubProcess() (*exec.Cmd, error) {
func (c *GitCommand) PrepareCommitSubProcess() *exec.Cmd {
return c.OSCommand.PrepareSubProcess("git", "commit")
}

Expand Down
17 changes: 5 additions & 12 deletions pkg/commands/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,21 @@ import (
"github.com/sirupsen/logrus"
)

func getDummyLog() *logrus.Entry {
func newDummyLog() *logrus.Entry {
log := logrus.New()
log.Out = ioutil.Discard
return log.WithField("test", "test")
}

func getDummyOSCommand() *OSCommand {
return &OSCommand{
Log: getDummyLog(),
Platform: getPlatform(),
}
}

func getDummyGitCommand() *GitCommand {
func newDummyGitCommand() *GitCommand {
return &GitCommand{
Log: getDummyLog(),
OSCommand: getDummyOSCommand(),
Log: newDummyLog(),
OSCommand: newDummyOSCommand(),
}
}

func TestDiff(t *testing.T) {
gitCommand := getDummyGitCommand()
gitCommand := newDummyGitCommand()
if err := test.GenerateRepo("lots_of_diffs.sh"); err != nil {
t.Error(err.Error())
}
Expand Down
87 changes: 38 additions & 49 deletions pkg/commands/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"os"
"os/exec"
"runtime"
"strings"

"github.com/davecgh/go-spew/spew"
Expand All @@ -25,26 +24,33 @@ type Platform struct {

// OSCommand holds all the os commands
type OSCommand struct {
Log *logrus.Entry
Platform *Platform
Log *logrus.Entry
Platform *Platform
command func(string, ...string) *exec.Cmd
getGlobalGitConfig func(string) (string, error)
getenv func(string) string
}

// NewOSCommand os command runner
func NewOSCommand(log *logrus.Entry) (*OSCommand, error) {
osCommand := &OSCommand{
Log: log,
Platform: getPlatform(),
func NewOSCommand(log *logrus.Entry) *OSCommand {
return &OSCommand{
Log: log,
Platform: getPlatform(),
command: exec.Command,
getGlobalGitConfig: gitconfig.Global,
getenv: os.Getenv,
}
return osCommand, nil
}

// RunCommandWithOutput wrapper around commands returning their output and error
func (c *OSCommand) RunCommandWithOutput(command string) (string, error) {
c.Log.WithField("command", command).Info("RunCommand")
splitCmd := str.ToArgv(command)
c.Log.Info(splitCmd)
cmdOut, err := exec.Command(splitCmd[0], splitCmd[1:]...).CombinedOutput()
return sanitisedCommandOutput(cmdOut, err)

return sanitisedCommandOutput(
c.command(splitCmd[0], splitCmd[1:]...).CombinedOutput(),
)
}

// RunCommand runs a command and just returns the error
Expand All @@ -59,10 +65,11 @@ func (c *OSCommand) RunDirectCommand(command string) (string, error) {
args := str.ToArgv(c.Platform.shellArg + " " + command)
c.Log.Info(spew.Sdump(args))

cmdOut, err := exec.
Command(c.Platform.shell, args...).
CombinedOutput()
return sanitisedCommandOutput(cmdOut, err)
return sanitisedCommandOutput(
exec.
Command(c.Platform.shell, args...).
CombinedOutput(),
)
}

func sanitisedCommandOutput(output []byte, err error) (string, error) {
Expand All @@ -75,33 +82,15 @@ func sanitisedCommandOutput(output []byte, err error) (string, error) {
return outputString, nil
}

func getPlatform() *Platform {
switch runtime.GOOS {
case "windows":
return &Platform{
os: "windows",
shell: "cmd",
shellArg: "/c",
escapedQuote: "\\\"",
}
default:
return &Platform{
os: runtime.GOOS,
shell: "bash",
shellArg: "-c",
escapedQuote: "\"",
}
}
}

// GetOpenCommand get open command
func (c *OSCommand) GetOpenCommand() (string, string, error) {
// getOpenCommand get open command
func (c *OSCommand) getOpenCommand() (string, string, error) {
//NextStep open equivalents: xdg-open (linux), cygstart (cygwin), open (OSX)
trailMap := map[string]string{
"xdg-open": " &>/dev/null &",
"cygstart": "",
"open": "",
}

for name, trail := range trailMap {
if err := c.RunCommand("which " + name); err == nil {
return name, trail, nil
Expand All @@ -126,24 +115,25 @@ func (c *OSCommand) SublimeOpenFile(filename string) (*exec.Cmd, error) {
}

// OpenFile opens a file with the given
func (c *OSCommand) OpenFile(filename string) (*exec.Cmd, error) {
cmdName, cmdTrail, err := c.GetOpenCommand()
func (c *OSCommand) OpenFile(filename string) error {
cmdName, cmdTrail, err := c.getOpenCommand()
if err != nil {
return nil, err
return err
}
err = c.RunCommand(cmdName + " " + c.Quote(filename) + cmdTrail) // TODO: test on linux
return nil, err

return c.RunCommand(cmdName + " " + c.Quote(filename) + cmdTrail) // TODO: test on linux
}

// EditFile opens a file in a subprocess using whatever editor is available,
// falling back to core.editor, VISUAL, EDITOR, then vi
func (c *OSCommand) EditFile(filename string) (*exec.Cmd, error) {
editor, _ := gitconfig.Global("core.editor")
editor, _ := c.getGlobalGitConfig("core.editor")

if editor == "" {
editor = os.Getenv("VISUAL")
editor = c.getenv("VISUAL")
}
if editor == "" {
editor = os.Getenv("EDITOR")
editor = c.getenv("EDITOR")
}
if editor == "" {
if err := c.RunCommand("which vi"); err == nil {
Expand All @@ -153,13 +143,13 @@ func (c *OSCommand) EditFile(filename string) (*exec.Cmd, error) {
if editor == "" {
return nil, errors.New("No editor defined in $VISUAL, $EDITOR, or git config")
}
return c.PrepareSubProcess(editor, filename)

return c.PrepareSubProcess(editor, filename), nil
}

// PrepareSubProcess iniPrepareSubProcessrocess then tells the Gui to switch to it
func (c *OSCommand) PrepareSubProcess(cmdName string, commandArgs ...string) (*exec.Cmd, error) {
subprocess := exec.Command(cmdName, commandArgs...)
return subprocess, nil
func (c *OSCommand) PrepareSubProcess(cmdName string, commandArgs ...string) *exec.Cmd {
return c.command(cmdName, commandArgs...)
}

// Quote wraps a message in platform-specific quotation marks
Expand All @@ -171,8 +161,7 @@ func (c *OSCommand) Quote(message string) string {
// Unquote removes wrapping quotations marks if they are present
// this is needed for removing quotes from staged filenames with spaces
func (c *OSCommand) Unquote(message string) string {
message = strings.Replace(message, `"`, "", -1)
return message
return strings.Replace(message, `"`, "", -1)
}

// AppendLineToFile adds a new line in file
Expand Down
16 changes: 16 additions & 0 deletions pkg/commands/os_default_platform.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// +build !windows

package commands

import (
"runtime"
)

func getPlatform() *Platform {
return &Platform{
os: runtime.GOOS,
shell: "bash",
shellArg: "-c",
escapedQuote: "\"",
}
}
Loading

0 comments on commit a1c6ada

Please sign in to comment.