Skip to content

Commit 51b705a

Browse files
authored
better fix for leak and cleanup of unnecessary string ptr (#508)
1 parent d222314 commit 51b705a

File tree

16 files changed

+79
-83
lines changed

16 files changed

+79
-83
lines changed

cmd/root.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func Execute() {
4848
}
4949
}
5050

51-
func createModel(repoPath *string, configPath string, debug bool) (ui.Model, *os.File) {
51+
func createModel(repoPath string, configPath string, debug bool) (ui.Model, *os.File) {
5252
var loggerFile *os.File
5353

5454
if debug {
@@ -60,8 +60,8 @@ func createModel(repoPath *string, configPath string, debug bool) (ui.Model, *os
6060
log.SetReportCaller(true)
6161
log.SetLevel(log.DebugLevel)
6262
log.Debug("Logging to debug.log")
63-
if repoPath != nil {
64-
log.Debug("Running in repo", "repo", *repoPath)
63+
if repoPath != "" {
64+
log.Debug("Running in repo", "repo", repoPath)
6565
}
6666
} else {
6767
loggerFile, _ = tea.LogToFile("debug.log", "debug")
@@ -128,17 +128,16 @@ func init() {
128128
)
129129

130130
rootCmd.Run = func(_ *cobra.Command, args []string) {
131-
var repo *string
131+
var repo string
132132
repos := config.IsFeatureEnabled(config.FF_REPO_VIEW)
133133
if repos && len(args) > 0 {
134-
repo = &args[0]
134+
repo = args[0]
135135
}
136136

137-
if repo == nil {
137+
if repo == "" {
138138
r, err := git.GetRepoInPwd()
139139
if err == nil && r != nil {
140-
p := r.Path()
141-
repo = &p
140+
repo = r.Path()
142141
}
143142
}
144143
debug, err := rootCmd.Flags().GetBool("debug")

config/parser.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,16 +365,16 @@ func (parser ConfigParser) createConfigFileIfMissing(
365365
return nil
366366
}
367367

368-
func (parser ConfigParser) getDefaultConfigFileOrCreateIfMissing(repoPath *string) (string, error) {
368+
func (parser ConfigParser) getDefaultConfigFileOrCreateIfMissing(repoPath string) (string, error) {
369369
var configFilePath string
370370
ghDashConfig := os.Getenv("GH_DASH_CONFIG")
371371

372372
// First try GH_DASH_CONFIG
373373
if ghDashConfig != "" {
374374
configFilePath = ghDashConfig
375375
// Then try to see if we're currently in a git repo
376-
} else if repoPath != nil {
377-
basename := *repoPath + "/." + DashDir
376+
} else if repoPath != "" {
377+
basename := repoPath + "/." + DashDir
378378
repoConfigYml := basename + ".yml"
379379
repoConfigYaml := basename + ".yaml"
380380
if _, err := os.Stat(repoConfigYml); err == nil {
@@ -463,7 +463,7 @@ func initParser() ConfigParser {
463463
return ConfigParser{}
464464
}
465465

466-
func ParseConfig(path string, repoPath *string) (Config, error) {
466+
func ParseConfig(path string, repoPath string) (Config, error) {
467467
parser := initParser()
468468

469469
var config Config

ui/components/branchsidebar/branchsidebar.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type Model struct {
1919
status *gitm.NameStatus
2020
}
2121

22-
func NewModel(ctx context.ProgramContext) Model {
22+
func NewModel(ctx *context.ProgramContext) Model {
2323
return Model{
2424
branch: nil,
2525
}
@@ -86,7 +86,7 @@ func (m *Model) SetRow(b *branch.BranchData) tea.Cmd {
8686
}
8787

8888
func (m *Model) refreshBranchStatusCmd() tea.Msg {
89-
status, err := git.GetStatus(*m.ctx.RepoPath)
89+
status, err := git.GetStatus(m.ctx.RepoPath)
9090
if err != nil {
9191
return nil
9292
}

ui/components/footer/footer.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ type Model struct {
2525
ShowConfirmQuit bool
2626
}
2727

28-
func NewModel(ctx context.ProgramContext) Model {
28+
func NewModel(ctx *context.ProgramContext) Model {
2929
help := bbHelp.New()
3030
help.ShowAll = true
3131
help.Styles = ctx.Styles.Help.BubbleStyles
3232
l := ""
3333
r := ""
3434
return Model{
35-
ctx: &ctx,
35+
ctx: ctx,
3636
help: help,
3737
leftSection: &l,
3838
rightSection: &r,
@@ -70,7 +70,7 @@ func (m Model) View() string {
7070
Foreground(m.ctx.Theme.SelectedBackground).
7171
Padding(0, 1).
7272
Render("? help")
73-
viewSwitcher := m.renderViewSwitcher(*m.ctx)
73+
viewSwitcher := m.renderViewSwitcher(m.ctx)
7474
leftSection := ""
7575
if m.leftSection != nil {
7676
leftSection = *m.leftSection
@@ -116,19 +116,18 @@ func (m *Model) UpdateProgramContext(ctx *context.ProgramContext) {
116116
m.help.Styles = ctx.Styles.Help.BubbleStyles
117117
}
118118

119-
func (m *Model) renderViewSwitcher(ctx context.ProgramContext) string {
119+
func (m *Model) renderViewSwitcher(ctx *context.ProgramContext) string {
120120
var view string
121121
if ctx.View == config.PRsView {
122122
view += " PRs"
123123
} else if ctx.View == config.IssuesView {
124124
view += " Issues"
125125
} else if ctx.View == config.RepoView {
126126
repo := m.ctx.RepoPath
127-
if m.ctx.RepoUrl != nil {
128-
shortName := git.GetRepoShortName(*m.ctx.RepoUrl)
129-
repo = &shortName
127+
if m.ctx.RepoUrl != "" {
128+
repo = git.GetRepoShortName(m.ctx.RepoUrl)
130129
}
131-
view += fmt.Sprintf(" %s", *repo)
130+
view += fmt.Sprintf(" %s", repo)
132131
}
133132

134133
var user string

ui/components/issuesidebar/issuesidebar.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ type Model struct {
3030
inputBox inputbox.Model
3131
}
3232

33-
func NewModel(ctx context.ProgramContext) Model {
34-
inputBox := inputbox.NewModel(&ctx)
33+
func NewModel(ctx *context.ProgramContext) Model {
34+
inputBox := inputbox.NewModel(ctx)
3535
inputBox.SetHeight(common.InputBoxHeight)
3636

3737
return Model{

ui/components/issuessection/issuessection.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,15 +325,15 @@ func (m *Model) ResetRows() {
325325
}
326326

327327
func FetchAllSections(
328-
ctx context.ProgramContext,
328+
ctx *context.ProgramContext,
329329
) (sections []section.Section, fetchAllCmd tea.Cmd) {
330330
sectionConfigs := ctx.Config.IssuesSections
331331
fetchIssuesCmds := make([]tea.Cmd, 0, len(sectionConfigs))
332332
sections = make([]section.Section, 0, len(sectionConfigs))
333333
for i, sectionConfig := range sectionConfigs {
334334
sectionModel := NewModel(
335335
i+1,
336-
&ctx,
336+
ctx,
337337
sectionConfig,
338338
time.Now(),
339339
) // 0 is the search section

ui/components/prsidebar/prsidebar.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ type Model struct {
3131
inputBox inputbox.Model
3232
}
3333

34-
func NewModel(ctx context.ProgramContext) Model {
35-
inputBox := inputbox.NewModel(&ctx)
34+
func NewModel(ctx *context.ProgramContext) Model {
35+
inputBox := inputbox.NewModel(ctx)
3636
inputBox.SetHeight(common.InputBoxHeight)
3737

3838
return Model{
@@ -320,9 +320,7 @@ func (m *Model) SetRow(d *data.PullRequestData) {
320320
if d == nil {
321321
m.pr = nil
322322
} else {
323-
// TODO: understand why not copying the ctx to a new var — causes a memory leak
324-
c := *m.ctx
325-
m.pr = &pr.PullRequest{Ctx: &c, Data: d}
323+
m.pr = &pr.PullRequest{Ctx: m.ctx, Data: d}
326324
}
327325
}
328326

ui/components/prssection/prssection.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,15 +433,15 @@ func (m *Model) ResetRows() {
433433
}
434434

435435
func FetchAllSections(
436-
ctx context.ProgramContext,
436+
ctx *context.ProgramContext,
437437
prs []section.Section,
438438
) (sections []section.Section, fetchAllCmd tea.Cmd) {
439439
fetchPRsCmds := make([]tea.Cmd, 0, len(ctx.Config.PRSections))
440440
sections = make([]section.Section, 0, len(ctx.Config.PRSections))
441441
for i, sectionConfig := range ctx.Config.PRSections {
442442
sectionModel := NewModel(
443443
i+1, // 0 is the search section
444-
&ctx,
444+
ctx,
445445
sectionConfig,
446446
time.Now(),
447447
)

ui/components/reposection/commands.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (m *Model) fastForward() (tea.Cmd, error) {
4040
startCmd := m.Ctx.StartTask(task)
4141
return tea.Batch(startCmd, func() tea.Msg {
4242
var err error
43-
repo, err := git.GetRepo(*m.Ctx.RepoPath)
43+
repo, err := git.GetRepo(m.Ctx.RepoPath)
4444
if err != nil {
4545
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
4646
}
@@ -62,7 +62,7 @@ func (m *Model) fastForward() (tea.Cmd, error) {
6262
if err != nil {
6363
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
6464
}
65-
repo, err = git.GetRepo(*m.Ctx.RepoPath)
65+
repo, err = git.GetRepo(m.Ctx.RepoPath)
6666
if err != nil {
6767
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
6868
}
@@ -108,14 +108,14 @@ func (m *Model) push(opts pushOptions) (tea.Cmd, error) {
108108
if len(b.Data.Remotes) == 0 {
109109
args = append(args, "--set-upstream")
110110
err = gitm.Push(
111-
*m.Ctx.RepoPath,
111+
m.Ctx.RepoPath,
112112
"origin",
113113
b.Data.Name,
114114
gitm.PushOptions{CommandOptions: gitm.CommandOptions{Args: args}},
115115
)
116116
} else {
117117
err = gitm.Push(
118-
*m.Ctx.RepoPath,
118+
m.Ctx.RepoPath,
119119
b.Data.Remotes[0],
120120
b.Data.Name,
121121
gitm.PushOptions{CommandOptions: gitm.CommandOptions{Args: args}},
@@ -124,7 +124,7 @@ func (m *Model) push(opts pushOptions) (tea.Cmd, error) {
124124
if err != nil {
125125
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
126126
}
127-
repo, err := git.GetRepo(*m.Ctx.RepoPath)
127+
repo, err := git.GetRepo(m.Ctx.RepoPath)
128128
if err != nil {
129129
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
130130
}
@@ -152,11 +152,11 @@ func (m *Model) checkout() (tea.Cmd, error) {
152152
}
153153
startCmd := m.Ctx.StartTask(task)
154154
return tea.Batch(startCmd, func() tea.Msg {
155-
err := gitm.Checkout(*m.Ctx.RepoPath, b.Data.Name)
155+
err := gitm.Checkout(m.Ctx.RepoPath, b.Data.Name)
156156
if err != nil {
157157
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
158158
}
159-
repo, err := git.GetRepo(*m.Ctx.RepoPath)
159+
repo, err := git.GetRepo(m.Ctx.RepoPath)
160160
if err != nil {
161161
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
162162
}
@@ -179,7 +179,7 @@ type repoMsg struct {
179179
func (m *Model) readRepoCmd() []tea.Cmd {
180180
cmds := make([]tea.Cmd, 0)
181181
branchesTaskId := fmt.Sprintf("fetching_branches_%d", time.Now().Unix())
182-
if m.Ctx.RepoPath != nil {
182+
if m.Ctx.RepoPath != "" {
183183
branchesTask := context.Task{
184184
Id: branchesTaskId,
185185
StartText: "Reading local branches",
@@ -191,7 +191,7 @@ func (m *Model) readRepoCmd() []tea.Cmd {
191191
cmds = append(cmds, bCmd)
192192
}
193193
cmds = append(cmds, func() tea.Msg {
194-
repo, err := git.GetRepo(*m.Ctx.RepoPath)
194+
repo, err := git.GetRepo(m.Ctx.RepoPath)
195195
if err != nil {
196196
return constants.TaskFinishedMsg{TaskId: branchesTaskId, Err: err}
197197
}
@@ -209,7 +209,7 @@ func (m *Model) readRepoCmd() []tea.Cmd {
209209
func (m *Model) fetchRepoCmd() []tea.Cmd {
210210
cmds := make([]tea.Cmd, 0)
211211
fetchTaskId := fmt.Sprintf("git_fetch_repo_%d", time.Now().Unix())
212-
if m.Ctx.RepoPath == nil {
212+
if m.Ctx.RepoPath == "" {
213213
return []tea.Cmd{}
214214
}
215215
fetchTask := context.Task{
@@ -221,7 +221,7 @@ func (m *Model) fetchRepoCmd() []tea.Cmd {
221221
}
222222
cmds = append(cmds, m.Ctx.StartTask(fetchTask))
223223
cmds = append(cmds, func() tea.Msg {
224-
repo, err := git.FetchRepo(*m.Ctx.RepoPath)
224+
repo, err := git.FetchRepo(m.Ctx.RepoPath)
225225
if err != nil {
226226
return constants.TaskFinishedMsg{TaskId: fetchTaskId, Err: err}
227227
}
@@ -251,7 +251,7 @@ func (m *Model) fetchPRsCmd() tea.Cmd {
251251
if limit == nil {
252252
limit = &m.Ctx.Config.Defaults.PrsLimit
253253
}
254-
res, err := data.FetchPullRequests(fmt.Sprintf("author:@me repo:%s", git.GetRepoShortName(*m.Ctx.RepoUrl)), *limit, nil)
254+
res, err := data.FetchPullRequests(fmt.Sprintf("author:@me repo:%s", git.GetRepoShortName(m.Ctx.RepoUrl)), *limit, nil)
255255
if err != nil {
256256
return constants.TaskFinishedMsg{
257257
SectionId: 0,
@@ -285,7 +285,7 @@ func (m *Model) fetchPRCmd(branch string) []tea.Cmd {
285285
}
286286
startCmd := m.Ctx.StartTask(task)
287287
return []tea.Cmd{startCmd, func() tea.Msg {
288-
res, err := data.FetchPullRequests(fmt.Sprintf("author:@me repo:%s head:%s", git.GetRepoShortName(*m.Ctx.RepoUrl), branch), 1, nil)
288+
res, err := data.FetchPullRequests(fmt.Sprintf("author:@me repo:%s head:%s", git.GetRepoShortName(m.Ctx.RepoUrl), branch), 1, nil)
289289
log.Debug("Fetching PRs", "res", res)
290290
if err != nil {
291291
return constants.TaskFinishedMsg{
@@ -385,11 +385,11 @@ func (m *Model) deleteBranch() tea.Cmd {
385385
}
386386
startCmd := m.Ctx.StartTask(task)
387387
return tea.Batch(startCmd, func() tea.Msg {
388-
err := gitm.DeleteBranch(*m.Ctx.RepoPath, b.Data.Name, gitm.DeleteBranchOptions{Force: true})
388+
err := gitm.DeleteBranch(m.Ctx.RepoPath, b.Data.Name, gitm.DeleteBranchOptions{Force: true})
389389
if err != nil {
390390
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
391391
}
392-
repo, err := git.GetRepo(*m.Ctx.RepoPath)
392+
repo, err := git.GetRepo(m.Ctx.RepoPath)
393393
if err != nil {
394394
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
395395
}
@@ -415,11 +415,11 @@ func (m *Model) newBranch(name string) tea.Cmd {
415415
}
416416
startCmd := m.Ctx.StartTask(task)
417417
return tea.Batch(startCmd, func() tea.Msg {
418-
err := gitm.Checkout(*m.Ctx.RepoPath, name, gitm.CheckoutOptions{BaseBranch: m.repo.HeadBranchName})
418+
err := gitm.Checkout(m.Ctx.RepoPath, name, gitm.CheckoutOptions{BaseBranch: m.repo.HeadBranchName})
419419
if err != nil {
420420
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
421421
}
422-
repo, err := git.GetRepo(*m.Ctx.RepoPath)
422+
repo, err := git.GetRepo(m.Ctx.RepoPath)
423423
if err != nil {
424424
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
425425
}

ui/components/reposection/reposection.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func (m *Model) View() string {
233233
}
234234

235235
return m.Ctx.Styles.Section.ContainerStyle.Render(
236-
lipgloss.JoinVertical(lipgloss.Left, m.SearchBar.View(*m.Ctx), view),
236+
lipgloss.JoinVertical(lipgloss.Left, m.SearchBar.View(m.Ctx), view),
237237
)
238238
}
239239

@@ -470,7 +470,7 @@ func (m *Model) FetchNextPageSectionRows() []tea.Cmd {
470470
}
471471

472472
var cmds []tea.Cmd
473-
if m.Ctx.RepoPath != nil {
473+
if m.Ctx.RepoPath != "" {
474474
cmds = append(cmds, m.readRepoCmd()...)
475475
cmds = append(cmds, m.fetchRepoCmd()...)
476476
cmds = append(cmds, m.fetchPRsCmd())
@@ -479,7 +479,7 @@ func (m *Model) FetchNextPageSectionRows() []tea.Cmd {
479479
return cmds
480480
}
481481

482-
func FetchAllBranches(ctx context.ProgramContext) (Model, tea.Cmd) {
482+
func FetchAllBranches(ctx *context.ProgramContext) (Model, tea.Cmd) {
483483
cmds := make([]tea.Cmd, 0)
484484

485485
t := config.RepoView
@@ -489,13 +489,13 @@ func FetchAllBranches(ctx context.ProgramContext) (Model, tea.Cmd) {
489489
}
490490
m := NewModel(
491491
0,
492-
&ctx,
492+
ctx,
493493
cfg,
494494
time.Now(),
495495
)
496496
m.refreshId = nextID()
497497

498-
if ctx.RepoPath != nil {
498+
if ctx.RepoPath != "" {
499499
cmds = append(cmds, m.readRepoCmd()...)
500500
cmds = append(cmds, m.fetchRepoCmd()...)
501501
cmds = append(cmds, m.fetchPRsCmd())

ui/components/search/search.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
5555
return m, cmd
5656
}
5757

58-
func (m Model) View(ctx context.ProgramContext) string {
58+
func (m Model) View(ctx *context.ProgramContext) string {
5959
return lipgloss.NewStyle().
6060
Width(ctx.MainContentWidth - 4).
6161
MaxHeight(3).

0 commit comments

Comments
 (0)