2018-05-19 03:16:34 +02:00
|
|
|
package main
|
|
|
|
|
2018-06-02 00:35:49 +02:00
|
|
|
import (
|
2018-08-07 10:05:43 +02:00
|
|
|
"errors"
|
2018-07-21 07:51:18 +02:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2018-08-09 04:25:32 +02:00
|
|
|
"io/ioutil"
|
2018-07-21 07:51:18 +02:00
|
|
|
"log"
|
|
|
|
"os"
|
2018-08-07 10:05:43 +02:00
|
|
|
"os/exec"
|
2018-07-21 07:51:18 +02:00
|
|
|
"os/user"
|
2018-08-09 05:21:30 +02:00
|
|
|
"path/filepath"
|
2018-07-21 07:51:18 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/fatih/color"
|
2018-08-07 10:05:43 +02:00
|
|
|
"github.com/jesseduffield/gocui"
|
2018-06-02 00:35:49 +02:00
|
|
|
)
|
|
|
|
|
2018-08-07 10:05:43 +02:00
|
|
|
// ErrSubProcess is raised when we are running a subprocess
|
2018-06-05 11:30:55 +02:00
|
|
|
var (
|
2018-08-07 10:05:43 +02:00
|
|
|
ErrSubprocess = errors.New("running subprocess")
|
|
|
|
subprocess *exec.Cmd
|
2018-08-08 11:13:13 +02:00
|
|
|
startTime time.Time
|
2018-08-08 10:57:27 +02:00
|
|
|
|
2018-08-08 14:48:37 +02:00
|
|
|
commit string
|
|
|
|
version = "unversioned"
|
2018-08-08 10:57:27 +02:00
|
|
|
|
2018-08-08 14:48:37 +02:00
|
|
|
date string
|
2018-08-08 11:46:21 +02:00
|
|
|
debuggingFlag = flag.Bool("debug", false, "a boolean")
|
|
|
|
versionFlag = flag.Bool("v", false, "Print the current version")
|
2018-06-05 11:30:55 +02:00
|
|
|
)
|
2018-05-27 08:32:09 +02:00
|
|
|
|
2018-06-05 11:32:03 +02:00
|
|
|
func homeDirectory() string {
|
2018-07-21 07:51:18 +02:00
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return usr.HomeDir
|
2018-06-05 11:32:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func devLog(objects ...interface{}) {
|
2018-07-21 07:51:18 +02:00
|
|
|
localLog(color.FgWhite, homeDirectory()+"/go/src/github.com/jesseduffield/lazygit/development.log", objects...)
|
2018-06-05 11:32:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func colorLog(colour color.Attribute, objects ...interface{}) {
|
2018-07-21 07:51:18 +02:00
|
|
|
localLog(colour, homeDirectory()+"/go/src/github.com/jesseduffield/lazygit/development.log", objects...)
|
2018-06-05 11:32:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func commandLog(objects ...interface{}) {
|
2018-07-21 07:51:18 +02:00
|
|
|
localLog(color.FgWhite, homeDirectory()+"/go/src/github.com/jesseduffield/lazygit/commands.log", objects...)
|
2018-06-05 11:32:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func localLog(colour color.Attribute, path string, objects ...interface{}) {
|
2018-08-08 11:46:21 +02:00
|
|
|
if !*debuggingFlag {
|
2018-07-21 07:51:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
f, _ := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0644)
|
|
|
|
defer f.Close()
|
|
|
|
for _, object := range objects {
|
|
|
|
colorFunction := color.New(colour).SprintFunc()
|
|
|
|
f.WriteString(colorFunction(fmt.Sprint(object)) + "\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func navigateToRepoRootDirectory() {
|
|
|
|
_, err := os.Stat(".git")
|
|
|
|
for os.IsNotExist(err) {
|
|
|
|
devLog("going up a directory to find the root")
|
|
|
|
os.Chdir("..")
|
|
|
|
_, err = os.Stat(".git")
|
|
|
|
}
|
2018-06-05 11:32:03 +02:00
|
|
|
}
|
|
|
|
|
2018-08-09 04:25:32 +02:00
|
|
|
// when building the binary, `version` is set as a compile-time variable, along
|
|
|
|
// with `date` and `commit`. If this program has been opened directly via go,
|
|
|
|
// we will populate the `version` with VERSION in the lazygit root directory
|
|
|
|
func fallbackVersion() string {
|
2018-08-09 04:55:41 +02:00
|
|
|
gopath := os.Getenv("GOPATH")
|
2018-08-09 05:21:30 +02:00
|
|
|
path := filepath.FromSlash(gopath + "/src/github.com/jesseduffield/lazygit/VERSION")
|
|
|
|
byteVersion, err := ioutil.ReadFile(path)
|
2018-08-09 04:25:32 +02:00
|
|
|
if err != nil {
|
2018-08-09 04:55:41 +02:00
|
|
|
return "unversioned"
|
2018-08-09 04:25:32 +02:00
|
|
|
}
|
|
|
|
return string(byteVersion)
|
|
|
|
}
|
|
|
|
|
2018-05-19 03:16:34 +02:00
|
|
|
func main() {
|
2018-08-07 16:15:23 +02:00
|
|
|
startTime = time.Now()
|
2018-07-21 07:51:18 +02:00
|
|
|
devLog("\n\n\n\n\n\n\n\n\n\n")
|
2018-08-07 16:15:23 +02:00
|
|
|
flag.Parse()
|
2018-08-09 04:25:32 +02:00
|
|
|
if version == "unversioned" {
|
|
|
|
version = fallbackVersion()
|
|
|
|
}
|
2018-08-07 16:15:23 +02:00
|
|
|
if *versionFlag {
|
2018-08-08 14:51:18 +02:00
|
|
|
fmt.Printf("commit=%s, build date=%s, version=%s", commit, date, version)
|
2018-08-07 16:15:23 +02:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2018-07-21 07:51:18 +02:00
|
|
|
verifyInGitRepo()
|
|
|
|
navigateToRepoRootDirectory()
|
2018-08-07 10:05:43 +02:00
|
|
|
for {
|
|
|
|
if err := run(); err != nil {
|
|
|
|
if err == gocui.ErrQuit {
|
|
|
|
break
|
|
|
|
} else if err == ErrSubprocess {
|
|
|
|
subprocess.Run()
|
|
|
|
} else {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-19 03:16:34 +02:00
|
|
|
}
|