2018-05-19 03:16:34 +02:00
package main
2018-06-02 00:35:49 +02:00
import (
2018-07-21 07:51:18 +02:00
"fmt"
2018-09-13 17:23:11 +02:00
"log"
2018-07-21 07:51:18 +02:00
"os"
2020-09-27 07:36:04 +02:00
"path/filepath"
2018-08-18 09:28:03 +02:00
"runtime"
2018-07-21 07:51:18 +02:00
2019-02-18 10:42:23 +02:00
"github.com/go-errors/errors"
2019-09-24 18:52:46 +02:00
"github.com/integrii/flaggy"
2018-08-12 11:31:27 +02:00
"github.com/jesseduffield/lazygit/pkg/app"
"github.com/jesseduffield/lazygit/pkg/config"
2020-09-27 08:17:26 +02:00
"github.com/jesseduffield/lazygit/pkg/env"
2018-06-02 00:35:49 +02:00
)
2018-06-05 11:30:55 +02:00
var (
2018-08-25 07:55:49 +02:00
commit string
version = "unversioned"
date string
buildSource = "unknown"
2018-06-05 11:30:55 +02:00
)
2018-05-27 08:32:09 +02:00
2018-05-19 03:16:34 +02:00
func main ( ) {
2019-09-24 18:52:46 +02:00
flaggy . DefaultParser . ShowVersionWithVersionFlag = false
2020-09-27 07:36:04 +02:00
repoPath := ""
2020-09-27 08:02:20 +02:00
flaggy . String ( & repoPath , "p" , "path" , "Path of git repo. (equivalent to --work-tree=<path> --git-dir=<path>/.git/)" )
2019-09-30 15:08:20 +02:00
2020-03-29 01:11:15 +02:00
filterPath := ""
flaggy . String ( & filterPath , "f" , "filter" , "Path to filter on in `git log -- <path>`. When in filter mode, the commits, reflog, and stash are filtered based on the given path, and some operations are restricted" )
2020-03-28 07:28:35 +02:00
2019-09-30 15:08:20 +02:00
dump := ""
flaggy . AddPositionalValue ( & dump , "gitargs" , 1 , false , "Todo file" )
flaggy . DefaultParser . PositionalFlags [ 0 ] . Hidden = true
2019-09-24 18:52:46 +02:00
versionFlag := false
flaggy . Bool ( & versionFlag , "v" , "version" , "Print the current version" )
debuggingFlag := false
2020-09-26 02:52:04 +02:00
flaggy . Bool ( & debuggingFlag , "d" , "debug" , "Run in debug mode with logging (see --logs flag below). Use the LOG_LEVEL env var to set the log level (debug/info/warn/error)" )
logFlag := false
flaggy . Bool ( & logFlag , "l" , "logs" , "Tail lazygit logs (intended to be used when `lazygit --debug` is called in a separate terminal tab)" )
2019-09-24 18:52:46 +02:00
configFlag := false
2020-09-18 00:39:16 +02:00
flaggy . Bool ( & configFlag , "c" , "config" , "Print the default config" )
2019-09-24 18:52:46 +02:00
2020-09-27 07:36:04 +02:00
workTree := ""
flaggy . String ( & workTree , "w" , "work-tree" , "equivalent of the --work-tree git argument" )
gitDir := ""
flaggy . String ( & gitDir , "g" , "git-dir" , "equivalent of the --git-dir git argument" )
2019-09-24 18:52:46 +02:00
flaggy . Parse ( )
2020-09-27 07:36:04 +02:00
if repoPath != "" {
if workTree != "" || gitDir != "" {
log . Fatal ( "--path option is incompatible with the --work-tree and --git-dir options" )
}
workTree = repoPath
gitDir = filepath . Join ( repoPath , ".git" )
}
if workTree != "" {
2020-09-27 08:17:26 +02:00
env . SetGitWorkTreeEnv ( workTree )
2020-09-27 07:36:04 +02:00
}
if gitDir != "" {
2020-09-27 08:17:26 +02:00
env . SetGitDirEnv ( gitDir )
2020-09-27 07:36:04 +02:00
}
2019-09-24 18:52:46 +02:00
if versionFlag {
2018-08-25 07:55:49 +02:00
fmt . Printf ( "commit=%s, build date=%s, build source=%s, version=%s, os=%s, arch=%s\n" , commit , date , buildSource , version , runtime . GOOS , runtime . GOARCH )
2018-08-07 16:15:23 +02:00
os . Exit ( 0 )
}
2018-08-27 10:55:56 +02:00
2019-09-24 18:52:46 +02:00
if configFlag {
2018-08-26 10:42:25 +02:00
fmt . Printf ( "%s\n" , config . GetDefaultConfig ( ) )
os . Exit ( 0 )
}
2019-09-24 18:52:46 +02:00
2020-09-26 02:23:10 +02:00
if logFlag {
app . TailLogs ( )
os . Exit ( 0 )
}
2020-09-27 07:36:04 +02:00
if workTree != "" {
if err := os . Chdir ( workTree ) ; err != nil {
2019-09-24 18:52:46 +02:00
log . Fatal ( err . Error ( ) )
}
}
appConfig , err := config . NewAppConfig ( "lazygit" , version , commit , date , buildSource , debuggingFlag )
2018-08-15 13:34:25 +02:00
if err != nil {
2018-09-13 17:23:11 +02:00
log . Fatal ( err . Error ( ) )
2018-08-12 11:31:27 +02:00
}
2018-08-15 13:34:25 +02:00
2020-03-29 01:11:15 +02:00
app , err := app . NewApp ( appConfig , filterPath )
2019-02-18 10:42:23 +02:00
if err == nil {
err = app . Run ( )
2018-08-18 11:43:58 +02:00
}
2018-08-29 21:47:48 +02:00
2019-02-18 10:42:23 +02:00
if err != nil {
2019-07-07 20:15:11 +02:00
if errorMessage , known := app . KnownError ( err ) ; known {
log . Fatal ( errorMessage )
}
2019-02-18 10:42:23 +02:00
newErr := errors . Wrap ( err , 0 )
stackTrace := newErr . ErrorStack ( )
app . Log . Error ( stackTrace )
log . Fatal ( fmt . Sprintf ( "%s\n\n%s" , app . Tr . SLocalize ( "ErrorOccurred" ) , stackTrace ) )
}
2018-05-19 03:16:34 +02:00
}