2018-05-19 11:16:34 +10:00
package main
2018-06-02 08:35:49 +10:00
import (
2020-10-03 14:54:55 +10:00
"bytes"
2018-07-21 15:51:18 +10:00
"fmt"
2018-09-14 00:23:11 +09:00
"log"
2018-07-21 15:51:18 +10:00
"os"
2020-09-27 15:36:04 +10:00
"path/filepath"
2018-08-18 17:28:03 +10:00
"runtime"
2022-06-09 19:52:08 +10:00
"strings"
2018-07-21 15:51:18 +10:00
2019-09-24 18:52:46 +02:00
"github.com/integrii/flaggy"
2018-08-12 19:31:27 +10:00
"github.com/jesseduffield/lazygit/pkg/app"
2022-05-07 15:23:08 +10:00
"github.com/jesseduffield/lazygit/pkg/app/daemon"
2018-08-12 19:31:27 +10:00
"github.com/jesseduffield/lazygit/pkg/config"
2020-09-27 16:17:26 +10:00
"github.com/jesseduffield/lazygit/pkg/env"
2022-06-09 19:52:08 +10:00
"github.com/jesseduffield/lazygit/pkg/gui/types"
2022-05-07 15:23:08 +10:00
"github.com/jesseduffield/lazygit/pkg/logs"
2020-10-03 14:54:55 +10:00
yaml "github.com/jesseduffield/yaml"
2018-06-02 08:35:49 +10:00
)
2018-06-05 19:30:55 +10:00
var (
2018-08-25 15:55:49 +10:00
commit string
version = "unversioned"
date string
buildSource = "unknown"
2018-06-05 19:30:55 +10:00
)
2018-05-27 16:32:09 +10:00
2018-05-19 11:16:34 +10:00
func main ( ) {
2019-09-24 18:52:46 +02:00
flaggy . DefaultParser . ShowVersionWithVersionFlag = false
2020-09-27 15:36:04 +10:00
repoPath := ""
2020-09-27 16:02:20 +10: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 10:11:15 +11: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 16:28:35 +11:00
2022-06-09 19:52:08 +10:00
gitArg := ""
flaggy . AddPositionalValue ( & gitArg , "git-arg" , 1 , false , "Panel to focus upon opening lazygit. Accepted values (based on git terminology): status, branch, log, stash. Ignored if --filter arg is passed." )
2019-09-24 18:52:46 +02:00
versionFlag := false
flaggy . Bool ( & versionFlag , "v" , "version" , "Print the current version" )
debuggingFlag := false
2020-09-26 10:52:04 +10: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 08:39:16 +10:00
flaggy . Bool ( & configFlag , "c" , "config" , "Print the default config" )
2019-09-24 18:52:46 +02:00
2020-10-04 09:53:56 +11:00
configDirFlag := false
2020-10-04 22:05:39 +11:00
flaggy . Bool ( & configDirFlag , "cd" , "print-config-dir" , "Print the config directory" )
useConfigDir := ""
flaggy . String ( & useConfigDir , "ucd" , "use-config-dir" , "override default config directory with provided directory" )
2020-10-04 09:53:56 +11:00
2020-09-27 15:36:04 +10: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" )
2021-07-27 22:03:37 +02:00
customConfig := ""
2022-04-03 15:19:15 -05:00
flaggy . String ( & customConfig , "ucf" , "use-config-file" , "Comma separated list to custom config file(s)" )
2021-07-27 22:03:37 +02:00
2019-09-24 18:52:46 +02:00
flaggy . Parse ( )
2020-09-27 15:36:04 +10:00
if repoPath != "" {
if workTree != "" || gitDir != "" {
log . Fatal ( "--path option is incompatible with the --work-tree and --git-dir options" )
}
2022-01-06 00:32:20 +01:00
absRepoPath , err := filepath . Abs ( repoPath )
if err != nil {
log . Fatal ( err )
}
workTree = absRepoPath
gitDir = filepath . Join ( absRepoPath , ".git" )
2020-09-27 15:36:04 +10:00
}
2021-07-27 22:03:37 +02:00
if customConfig != "" {
os . Setenv ( "LG_CONFIG_FILE" , customConfig )
}
2020-10-04 22:05:39 +11:00
if useConfigDir != "" {
os . Setenv ( "CONFIG_DIR" , useConfigDir )
}
2020-09-27 15:36:04 +10:00
if workTree != "" {
2020-09-27 16:17:26 +10:00
env . SetGitWorkTreeEnv ( workTree )
2020-09-27 15:36:04 +10:00
}
if gitDir != "" {
2020-09-27 16:17:26 +10:00
env . SetGitDirEnv ( gitDir )
2020-09-27 15:36:04 +10:00
}
2019-09-24 18:52:46 +02:00
if versionFlag {
2018-08-25 15:55:49 +10: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 18:55:56 +10:00
2019-09-24 18:52:46 +02:00
if configFlag {
2020-10-03 14:54:55 +10:00
var buf bytes . Buffer
encoder := yaml . NewEncoder ( & buf )
err := encoder . Encode ( config . GetDefaultConfig ( ) )
if err != nil {
log . Fatal ( err . Error ( ) )
}
2020-10-04 09:53:56 +11:00
fmt . Printf ( "%s\n" , buf . String ( ) )
os . Exit ( 0 )
}
if configDirFlag {
2020-11-22 14:51:48 +01:00
fmt . Printf ( "%s\n" , config . ConfigDir ( ) )
2018-08-26 10:42:25 +02:00
os . Exit ( 0 )
}
2019-09-24 18:52:46 +02:00
2020-09-26 10:23:10 +10:00
if logFlag {
2022-05-07 15:23:08 +10:00
logs . TailLogs ( )
2020-09-26 10:23:10 +10:00
os . Exit ( 0 )
}
2020-09-27 15:36:04 +10:00
if workTree != "" {
if err := os . Chdir ( workTree ) ; err != nil {
2019-09-24 18:52:46 +02:00
log . Fatal ( err . Error ( ) )
}
}
2022-04-01 23:38:41 +09:00
tempDir , err := os . MkdirTemp ( "" , "lazygit-*" )
if err != nil {
log . Fatal ( err . Error ( ) )
}
defer os . RemoveAll ( tempDir )
appConfig , err := config . NewAppConfig ( "lazygit" , version , commit , date , buildSource , debuggingFlag , tempDir )
2018-08-15 21:34:25 +10:00
if err != nil {
2018-09-14 00:23:11 +09:00
log . Fatal ( err . Error ( ) )
2018-08-12 19:31:27 +10:00
}
2018-08-15 21:34:25 +10:00
2022-05-07 15:23:08 +10:00
common , err := app . NewCommon ( appConfig )
2019-02-18 19:42:23 +11:00
if err != nil {
2022-05-07 15:23:08 +10:00
log . Fatal ( err )
}
2019-02-18 19:42:23 +11:00
2022-05-07 15:23:08 +10:00
if daemon . InDaemonMode ( ) {
daemon . Handle ( common )
return
2019-02-18 19:42:23 +11:00
}
2022-05-07 15:23:08 +10:00
2022-06-09 19:52:08 +10:00
parsedGitArg := parseGitArg ( gitArg )
app . Run ( appConfig , common , types . NewStartArgs ( filterPath , parsedGitArg ) )
}
func parseGitArg ( gitArg string ) types . GitArg {
typedArg := types . GitArg ( gitArg )
// using switch so that linter catches when a new git arg value is defined but not handled here
switch typedArg {
case types . GitArgNone , types . GitArgStatus , types . GitArgBranch , types . GitArgLog , types . GitArgStash :
return typedArg
}
permittedValues := [ ] string {
string ( types . GitArgStatus ) ,
string ( types . GitArgBranch ) ,
string ( types . GitArgLog ) ,
string ( types . GitArgStash ) ,
}
log . Fatalf ( "Invalid git arg value: '%s'. Must be one of the following values: %s. e.g. 'lazygit status'. See 'lazygit --help'." ,
gitArg ,
strings . Join ( permittedValues , ", " ) ,
)
panic ( "unreachable" )
2018-05-19 11:16:34 +10:00
}