Skip to content

Commit 304d743

Browse files
committed
refactor to ensure code doesn't depend on integration code
1 parent 2bdefe2 commit 304d743

File tree

16 files changed

+203
-169
lines changed

16 files changed

+203
-169
lines changed

main.go

+4-131
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,24 @@
11
package main
22

33
import (
4-
"os"
5-
"runtime/debug"
6-
7-
"github.com/integrii/flaggy"
84
"github.com/jesseduffield/lazygit/pkg/app"
9-
"github.com/jesseduffield/lazygit/pkg/integration"
10-
integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types"
11-
"github.com/jesseduffield/lazygit/pkg/utils"
12-
"github.com/samber/lo"
135
)
146

15-
const DEFAULT_VERSION = "unversioned"
16-
17-
// These values may be set by the build script.
18-
// we'll overwrite them if they haven't been set by the build script and if Go itself has set corresponding values in the binary
7+
// These values may be set by the build script via the LDFLAGS argument
198
var (
209
commit string
21-
version = DEFAULT_VERSION
2210
date string
11+
version string
2312
buildSource = "unknown"
2413
)
2514

2615
func main() {
27-
cliArgs := parseCliArgsAndEnvVars()
28-
buildInfo := getBuildInfo()
29-
integrationTest := getIntegrationTest()
30-
31-
app.Start(cliArgs, buildInfo, integrationTest)
32-
}
33-
34-
func parseCliArgsAndEnvVars() *app.CliArgs {
35-
flaggy.DefaultParser.ShowVersionWithVersionFlag = false
36-
37-
repoPath := ""
38-
flaggy.String(&repoPath, "p", "path", "Path of git repo. (equivalent to --work-tree=<path> --git-dir=<path>/.git/)")
39-
40-
filterPath := ""
41-
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")
42-
43-
gitArg := ""
44-
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.")
45-
46-
printVersionInfo := false
47-
flaggy.Bool(&printVersionInfo, "v", "version", "Print the current version")
48-
49-
debug := false
50-
flaggy.Bool(&debug, "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)")
51-
52-
tailLogs := false
53-
flaggy.Bool(&tailLogs, "l", "logs", "Tail lazygit logs (intended to be used when `lazygit --debug` is called in a separate terminal tab)")
54-
55-
printDefaultConfig := false
56-
flaggy.Bool(&printDefaultConfig, "c", "config", "Print the default config")
57-
58-
printConfigDir := false
59-
flaggy.Bool(&printConfigDir, "cd", "print-config-dir", "Print the config directory")
60-
61-
useConfigDir := ""
62-
flaggy.String(&useConfigDir, "ucd", "use-config-dir", "override default config directory with provided directory")
63-
64-
workTree := ""
65-
flaggy.String(&workTree, "w", "work-tree", "equivalent of the --work-tree git argument")
66-
67-
gitDir := ""
68-
flaggy.String(&gitDir, "g", "git-dir", "equivalent of the --git-dir git argument")
69-
70-
customConfigFile := ""
71-
flaggy.String(&customConfigFile, "ucf", "use-config-file", "Comma separated list to custom config file(s)")
72-
73-
flaggy.Parse()
74-
75-
if os.Getenv("DEBUG") == "TRUE" {
76-
debug = true
77-
}
78-
79-
return &app.CliArgs{
80-
RepoPath: repoPath,
81-
FilterPath: filterPath,
82-
GitArg: gitArg,
83-
PrintVersionInfo: printVersionInfo,
84-
Debug: debug,
85-
TailLogs: tailLogs,
86-
PrintDefaultConfig: printDefaultConfig,
87-
PrintConfigDir: printConfigDir,
88-
UseConfigDir: useConfigDir,
89-
WorkTree: workTree,
90-
GitDir: gitDir,
91-
CustomConfigFile: customConfigFile,
92-
}
93-
}
94-
95-
func getBuildInfo() *app.BuildInfo {
96-
buildInfo := &app.BuildInfo{
16+
ldFlagsBuildInfo := &app.BuildInfo{
9717
Commit: commit,
9818
Date: date,
9919
Version: version,
10020
BuildSource: buildSource,
10121
}
10222

103-
// if the version has already been set by build flags then we'll honour that.
104-
// chances are it's something like v0.31.0 which is more informative than a
105-
// commit hash.
106-
if buildInfo.Version != DEFAULT_VERSION {
107-
return buildInfo
108-
}
109-
110-
goBuildInfo, ok := debug.ReadBuildInfo()
111-
if !ok {
112-
return buildInfo
113-
}
114-
115-
revision, ok := lo.Find(goBuildInfo.Settings, func(setting debug.BuildSetting) bool {
116-
return setting.Key == "vcs.revision"
117-
})
118-
if ok {
119-
buildInfo.Commit = revision.Value
120-
// if lazygit was built from source we'll show the version as the
121-
// abbreviated commit hash
122-
buildInfo.Version = utils.ShortSha(revision.Value)
123-
}
124-
125-
// if version hasn't been set we assume that neither has the date
126-
time, ok := lo.Find(goBuildInfo.Settings, func(setting debug.BuildSetting) bool {
127-
return setting.Key == "vcs.time"
128-
})
129-
if ok {
130-
buildInfo.Date = time.Value
131-
}
132-
133-
return buildInfo
134-
}
135-
136-
func getIntegrationTest() integrationTypes.IntegrationTest {
137-
integrationTestName := os.Getenv("LAZYGIT_TEST_NAME")
138-
if integrationTestName == "" {
139-
return nil
140-
}
141-
142-
// unsetting so that if we run lazygit in as a 'daemon' we don't think we're trying to run a test again
143-
os.Unsetenv("LAZYGIT_TEST_NAME")
144-
for _, candidateTest := range integration.Tests {
145-
if candidateTest.Name() == integrationTestName {
146-
return candidateTest
147-
}
148-
}
149-
150-
panic("Could not find integration test with name: " + integrationTestName)
23+
app.Start(ldFlagsBuildInfo, nil)
15124
}

pkg/app/entry_point.go

+110-2
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@ import (
77
"os"
88
"path/filepath"
99
"runtime"
10+
"runtime/debug"
1011
"strings"
1112

13+
"github.com/integrii/flaggy"
1214
"github.com/jesseduffield/lazygit/pkg/app/daemon"
1315
appTypes "github.com/jesseduffield/lazygit/pkg/app/types"
1416
"github.com/jesseduffield/lazygit/pkg/config"
1517
"github.com/jesseduffield/lazygit/pkg/env"
1618
integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types"
1719
"github.com/jesseduffield/lazygit/pkg/logs"
20+
"github.com/jesseduffield/lazygit/pkg/utils"
21+
"github.com/samber/lo"
1822
"gopkg.in/yaml.v3"
1923
)
2024

21-
type CliArgs struct {
25+
type cliArgs struct {
2226
RepoPath string
2327
FilterPath string
2428
GitArg string
@@ -40,7 +44,10 @@ type BuildInfo struct {
4044
BuildSource string
4145
}
4246

43-
func Start(cliArgs *CliArgs, buildInfo *BuildInfo, integrationTest integrationTypes.IntegrationTest) {
47+
func Start(buildInfo *BuildInfo, integrationTest integrationTypes.IntegrationTest) {
48+
cliArgs := parseCliArgsAndEnvVars()
49+
mergeBuildInfo(buildInfo)
50+
4451
if cliArgs.RepoPath != "" {
4552
if cliArgs.WorkTree != "" || cliArgs.GitDir != "" {
4653
log.Fatal("--path option is incompatible with the --work-tree and --git-dir options")
@@ -132,6 +139,67 @@ func Start(cliArgs *CliArgs, buildInfo *BuildInfo, integrationTest integrationTy
132139
Run(appConfig, common, appTypes.NewStartArgs(cliArgs.FilterPath, parsedGitArg, integrationTest))
133140
}
134141

142+
func parseCliArgsAndEnvVars() *cliArgs {
143+
flaggy.DefaultParser.ShowVersionWithVersionFlag = false
144+
145+
repoPath := ""
146+
flaggy.String(&repoPath, "p", "path", "Path of git repo. (equivalent to --work-tree=<path> --git-dir=<path>/.git/)")
147+
148+
filterPath := ""
149+
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")
150+
151+
gitArg := ""
152+
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.")
153+
154+
printVersionInfo := false
155+
flaggy.Bool(&printVersionInfo, "v", "version", "Print the current version")
156+
157+
debug := false
158+
flaggy.Bool(&debug, "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)")
159+
160+
tailLogs := false
161+
flaggy.Bool(&tailLogs, "l", "logs", "Tail lazygit logs (intended to be used when `lazygit --debug` is called in a separate terminal tab)")
162+
163+
printDefaultConfig := false
164+
flaggy.Bool(&printDefaultConfig, "c", "config", "Print the default config")
165+
166+
printConfigDir := false
167+
flaggy.Bool(&printConfigDir, "cd", "print-config-dir", "Print the config directory")
168+
169+
useConfigDir := ""
170+
flaggy.String(&useConfigDir, "ucd", "use-config-dir", "override default config directory with provided directory")
171+
172+
workTree := ""
173+
flaggy.String(&workTree, "w", "work-tree", "equivalent of the --work-tree git argument")
174+
175+
gitDir := ""
176+
flaggy.String(&gitDir, "g", "git-dir", "equivalent of the --git-dir git argument")
177+
178+
customConfigFile := ""
179+
flaggy.String(&customConfigFile, "ucf", "use-config-file", "Comma separated list to custom config file(s)")
180+
181+
flaggy.Parse()
182+
183+
if os.Getenv("DEBUG") == "TRUE" {
184+
debug = true
185+
}
186+
187+
return &cliArgs{
188+
RepoPath: repoPath,
189+
FilterPath: filterPath,
190+
GitArg: gitArg,
191+
PrintVersionInfo: printVersionInfo,
192+
Debug: debug,
193+
TailLogs: tailLogs,
194+
PrintDefaultConfig: printDefaultConfig,
195+
PrintConfigDir: printConfigDir,
196+
UseConfigDir: useConfigDir,
197+
WorkTree: workTree,
198+
GitDir: gitDir,
199+
CustomConfigFile: customConfigFile,
200+
}
201+
}
202+
135203
func parseGitArg(gitArg string) appTypes.GitArg {
136204
typedArg := appTypes.GitArg(gitArg)
137205

@@ -155,3 +223,43 @@ func parseGitArg(gitArg string) appTypes.GitArg {
155223

156224
panic("unreachable")
157225
}
226+
227+
// the buildInfo struct we get passed in is based on what's baked into the lazygit
228+
// binary via the LDFLAGS argument. Some lazygit distributions will make use of these
229+
// arguments and some will not. Go recently started baking in build info
230+
// into the binary by default e.g. the git commit hash. So in this function
231+
// we merge the two together, giving priority to the stuff set by LDFLAGS.
232+
// Note: this mutates the argument passed in
233+
func mergeBuildInfo(buildInfo *BuildInfo) {
234+
// if the version has already been set by build flags then we'll honour that.
235+
// chances are it's something like v0.31.0 which is more informative than a
236+
// commit hash.
237+
if buildInfo.Version != "" {
238+
return
239+
}
240+
241+
buildInfo.Version = "unversioned"
242+
243+
goBuildInfo, ok := debug.ReadBuildInfo()
244+
if !ok {
245+
return
246+
}
247+
248+
revision, ok := lo.Find(goBuildInfo.Settings, func(setting debug.BuildSetting) bool {
249+
return setting.Key == "vcs.revision"
250+
})
251+
if ok {
252+
buildInfo.Commit = revision.Value
253+
// if lazygit was built from source we'll show the version as the
254+
// abbreviated commit hash
255+
buildInfo.Version = utils.ShortSha(revision.Value)
256+
}
257+
258+
// if version hasn't been set we assume that neither has the date
259+
time, ok := lo.Find(goBuildInfo.Settings, func(setting debug.BuildSetting) bool {
260+
return setting.Key == "vcs.time"
261+
})
262+
if ok {
263+
buildInfo.Date = time.Value
264+
}
265+
}

pkg/gui/gui_adapter.go renamed to pkg/gui/gui_driver.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import (
1414

1515
// this gives our integration test a way of interacting with the gui for sending keypresses
1616
// and reading state.
17-
type GuiAdapter struct {
17+
type GuiDriver struct {
1818
gui *Gui
1919
}
2020

21-
var _ integrationTypes.GuiAdapter = &GuiAdapter{}
21+
var _ integrationTypes.GuiDriver = &GuiDriver{}
2222

23-
func (self *GuiAdapter) PressKey(keyStr string) {
23+
func (self *GuiDriver) PressKey(keyStr string) {
2424
key := keybindings.GetKey(keyStr)
2525

2626
var r rune
@@ -39,35 +39,35 @@ func (self *GuiAdapter) PressKey(keyStr string) {
3939
)
4040
}
4141

42-
func (self *GuiAdapter) Keys() config.KeybindingConfig {
42+
func (self *GuiDriver) Keys() config.KeybindingConfig {
4343
return self.gui.Config.GetUserConfig().Keybinding
4444
}
4545

46-
func (self *GuiAdapter) CurrentContext() types.Context {
46+
func (self *GuiDriver) CurrentContext() types.Context {
4747
return self.gui.c.CurrentContext()
4848
}
4949

50-
func (self *GuiAdapter) Model() *types.Model {
50+
func (self *GuiDriver) Model() *types.Model {
5151
return self.gui.State.Model
5252
}
5353

54-
func (self *GuiAdapter) Fail(message string) {
54+
func (self *GuiDriver) Fail(message string) {
5555
self.gui.g.Close()
5656
// need to give the gui time to close
5757
time.Sleep(time.Millisecond * 100)
5858
panic(message)
5959
}
6060

6161
// logs to the normal place that you log to i.e. viewable with `lazygit --logs`
62-
func (self *GuiAdapter) Log(message string) {
62+
func (self *GuiDriver) Log(message string) {
6363
self.gui.c.Log.Warn(message)
6464
}
6565

6666
// logs in the actual UI (in the commands panel)
67-
func (self *GuiAdapter) LogUI(message string) {
67+
func (self *GuiDriver) LogUI(message string) {
6868
self.gui.c.LogAction(message)
6969
}
7070

71-
func (self *GuiAdapter) CheckedOutRef() *models.Branch {
71+
func (self *GuiDriver) CheckedOutRef() *models.Branch {
7272
return self.gui.helpers.Refs.GetCheckedOutRef()
7373
}

pkg/gui/test_mode.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ import (
1414
)
1515

1616
type IntegrationTest interface {
17-
Run(guiAdapter *GuiAdapter)
17+
Run(guiAdapter *GuiDriver)
1818
}
1919

2020
func (gui *Gui) handleTestMode(test integrationTypes.IntegrationTest) {
2121
if test != nil {
2222
go func() {
2323
time.Sleep(time.Millisecond * 100)
2424

25-
test.Run(&GuiAdapter{gui: gui})
25+
test.Run(&GuiDriver{gui: gui})
2626

2727
gui.g.Update(func(*gocui.Gui) error {
2828
return gocui.ErrQuit

0 commit comments

Comments
 (0)