Skip to content

Commit

Permalink
better fix for leak and cleanup of unnecessary string ptr (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly authored Jan 16, 2025
1 parent d222314 commit 51b705a
Show file tree
Hide file tree
Showing 16 changed files with 79 additions and 83 deletions.
15 changes: 7 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Execute() {
}
}

func createModel(repoPath *string, configPath string, debug bool) (ui.Model, *os.File) {
func createModel(repoPath string, configPath string, debug bool) (ui.Model, *os.File) {
var loggerFile *os.File

if debug {
Expand All @@ -60,8 +60,8 @@ func createModel(repoPath *string, configPath string, debug bool) (ui.Model, *os
log.SetReportCaller(true)
log.SetLevel(log.DebugLevel)
log.Debug("Logging to debug.log")
if repoPath != nil {
log.Debug("Running in repo", "repo", *repoPath)
if repoPath != "" {
log.Debug("Running in repo", "repo", repoPath)
}
} else {
loggerFile, _ = tea.LogToFile("debug.log", "debug")
Expand Down Expand Up @@ -128,17 +128,16 @@ func init() {
)

rootCmd.Run = func(_ *cobra.Command, args []string) {
var repo *string
var repo string
repos := config.IsFeatureEnabled(config.FF_REPO_VIEW)
if repos && len(args) > 0 {
repo = &args[0]
repo = args[0]
}

if repo == nil {
if repo == "" {
r, err := git.GetRepoInPwd()
if err == nil && r != nil {
p := r.Path()
repo = &p
repo = r.Path()
}
}
debug, err := rootCmd.Flags().GetBool("debug")
Expand Down
8 changes: 4 additions & 4 deletions config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,16 +365,16 @@ func (parser ConfigParser) createConfigFileIfMissing(
return nil
}

func (parser ConfigParser) getDefaultConfigFileOrCreateIfMissing(repoPath *string) (string, error) {
func (parser ConfigParser) getDefaultConfigFileOrCreateIfMissing(repoPath string) (string, error) {
var configFilePath string
ghDashConfig := os.Getenv("GH_DASH_CONFIG")

// First try GH_DASH_CONFIG
if ghDashConfig != "" {
configFilePath = ghDashConfig
// Then try to see if we're currently in a git repo
} else if repoPath != nil {
basename := *repoPath + "/." + DashDir
} else if repoPath != "" {
basename := repoPath + "/." + DashDir
repoConfigYml := basename + ".yml"
repoConfigYaml := basename + ".yaml"
if _, err := os.Stat(repoConfigYml); err == nil {
Expand Down Expand Up @@ -463,7 +463,7 @@ func initParser() ConfigParser {
return ConfigParser{}
}

func ParseConfig(path string, repoPath *string) (Config, error) {
func ParseConfig(path string, repoPath string) (Config, error) {
parser := initParser()

var config Config
Expand Down
4 changes: 2 additions & 2 deletions ui/components/branchsidebar/branchsidebar.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Model struct {
status *gitm.NameStatus
}

func NewModel(ctx context.ProgramContext) Model {
func NewModel(ctx *context.ProgramContext) Model {
return Model{
branch: nil,
}
Expand Down Expand Up @@ -86,7 +86,7 @@ func (m *Model) SetRow(b *branch.BranchData) tea.Cmd {
}

func (m *Model) refreshBranchStatusCmd() tea.Msg {
status, err := git.GetStatus(*m.ctx.RepoPath)
status, err := git.GetStatus(m.ctx.RepoPath)
if err != nil {
return nil
}
Expand Down
15 changes: 7 additions & 8 deletions ui/components/footer/footer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ type Model struct {
ShowConfirmQuit bool
}

func NewModel(ctx context.ProgramContext) Model {
func NewModel(ctx *context.ProgramContext) Model {
help := bbHelp.New()
help.ShowAll = true
help.Styles = ctx.Styles.Help.BubbleStyles
l := ""
r := ""
return Model{
ctx: &ctx,
ctx: ctx,
help: help,
leftSection: &l,
rightSection: &r,
Expand Down Expand Up @@ -70,7 +70,7 @@ func (m Model) View() string {
Foreground(m.ctx.Theme.SelectedBackground).
Padding(0, 1).
Render("? help")
viewSwitcher := m.renderViewSwitcher(*m.ctx)
viewSwitcher := m.renderViewSwitcher(m.ctx)
leftSection := ""
if m.leftSection != nil {
leftSection = *m.leftSection
Expand Down Expand Up @@ -116,19 +116,18 @@ func (m *Model) UpdateProgramContext(ctx *context.ProgramContext) {
m.help.Styles = ctx.Styles.Help.BubbleStyles
}

func (m *Model) renderViewSwitcher(ctx context.ProgramContext) string {
func (m *Model) renderViewSwitcher(ctx *context.ProgramContext) string {
var view string
if ctx.View == config.PRsView {
view += " PRs"
} else if ctx.View == config.IssuesView {
view += " Issues"
} else if ctx.View == config.RepoView {
repo := m.ctx.RepoPath
if m.ctx.RepoUrl != nil {
shortName := git.GetRepoShortName(*m.ctx.RepoUrl)
repo = &shortName
if m.ctx.RepoUrl != "" {
repo = git.GetRepoShortName(m.ctx.RepoUrl)
}
view += fmt.Sprintf(" %s", *repo)
view += fmt.Sprintf(" %s", repo)
}

var user string
Expand Down
4 changes: 2 additions & 2 deletions ui/components/issuesidebar/issuesidebar.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ type Model struct {
inputBox inputbox.Model
}

func NewModel(ctx context.ProgramContext) Model {
inputBox := inputbox.NewModel(&ctx)
func NewModel(ctx *context.ProgramContext) Model {
inputBox := inputbox.NewModel(ctx)
inputBox.SetHeight(common.InputBoxHeight)

return Model{
Expand Down
4 changes: 2 additions & 2 deletions ui/components/issuessection/issuessection.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,15 +325,15 @@ func (m *Model) ResetRows() {
}

func FetchAllSections(
ctx context.ProgramContext,
ctx *context.ProgramContext,
) (sections []section.Section, fetchAllCmd tea.Cmd) {
sectionConfigs := ctx.Config.IssuesSections
fetchIssuesCmds := make([]tea.Cmd, 0, len(sectionConfigs))
sections = make([]section.Section, 0, len(sectionConfigs))
for i, sectionConfig := range sectionConfigs {
sectionModel := NewModel(
i+1,
&ctx,
ctx,
sectionConfig,
time.Now(),
) // 0 is the search section
Expand Down
8 changes: 3 additions & 5 deletions ui/components/prsidebar/prsidebar.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ type Model struct {
inputBox inputbox.Model
}

func NewModel(ctx context.ProgramContext) Model {
inputBox := inputbox.NewModel(&ctx)
func NewModel(ctx *context.ProgramContext) Model {
inputBox := inputbox.NewModel(ctx)
inputBox.SetHeight(common.InputBoxHeight)

return Model{
Expand Down Expand Up @@ -320,9 +320,7 @@ func (m *Model) SetRow(d *data.PullRequestData) {
if d == nil {
m.pr = nil
} else {
// TODO: understand why not copying the ctx to a new var — causes a memory leak
c := *m.ctx
m.pr = &pr.PullRequest{Ctx: &c, Data: d}
m.pr = &pr.PullRequest{Ctx: m.ctx, Data: d}
}
}

Expand Down
4 changes: 2 additions & 2 deletions ui/components/prssection/prssection.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,15 +433,15 @@ func (m *Model) ResetRows() {
}

func FetchAllSections(
ctx context.ProgramContext,
ctx *context.ProgramContext,
prs []section.Section,
) (sections []section.Section, fetchAllCmd tea.Cmd) {
fetchPRsCmds := make([]tea.Cmd, 0, len(ctx.Config.PRSections))
sections = make([]section.Section, 0, len(ctx.Config.PRSections))
for i, sectionConfig := range ctx.Config.PRSections {
sectionModel := NewModel(
i+1, // 0 is the search section
&ctx,
ctx,
sectionConfig,
time.Now(),
)
Expand Down
34 changes: 17 additions & 17 deletions ui/components/reposection/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (m *Model) fastForward() (tea.Cmd, error) {
startCmd := m.Ctx.StartTask(task)
return tea.Batch(startCmd, func() tea.Msg {
var err error
repo, err := git.GetRepo(*m.Ctx.RepoPath)
repo, err := git.GetRepo(m.Ctx.RepoPath)
if err != nil {
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
}
Expand All @@ -62,7 +62,7 @@ func (m *Model) fastForward() (tea.Cmd, error) {
if err != nil {
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
}
repo, err = git.GetRepo(*m.Ctx.RepoPath)
repo, err = git.GetRepo(m.Ctx.RepoPath)
if err != nil {
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
}
Expand Down Expand Up @@ -108,14 +108,14 @@ func (m *Model) push(opts pushOptions) (tea.Cmd, error) {
if len(b.Data.Remotes) == 0 {
args = append(args, "--set-upstream")
err = gitm.Push(
*m.Ctx.RepoPath,
m.Ctx.RepoPath,
"origin",
b.Data.Name,
gitm.PushOptions{CommandOptions: gitm.CommandOptions{Args: args}},
)
} else {
err = gitm.Push(
*m.Ctx.RepoPath,
m.Ctx.RepoPath,
b.Data.Remotes[0],
b.Data.Name,
gitm.PushOptions{CommandOptions: gitm.CommandOptions{Args: args}},
Expand All @@ -124,7 +124,7 @@ func (m *Model) push(opts pushOptions) (tea.Cmd, error) {
if err != nil {
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
}
repo, err := git.GetRepo(*m.Ctx.RepoPath)
repo, err := git.GetRepo(m.Ctx.RepoPath)
if err != nil {
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
}
Expand Down Expand Up @@ -152,11 +152,11 @@ func (m *Model) checkout() (tea.Cmd, error) {
}
startCmd := m.Ctx.StartTask(task)
return tea.Batch(startCmd, func() tea.Msg {
err := gitm.Checkout(*m.Ctx.RepoPath, b.Data.Name)
err := gitm.Checkout(m.Ctx.RepoPath, b.Data.Name)
if err != nil {
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
}
repo, err := git.GetRepo(*m.Ctx.RepoPath)
repo, err := git.GetRepo(m.Ctx.RepoPath)
if err != nil {
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
}
Expand All @@ -179,7 +179,7 @@ type repoMsg struct {
func (m *Model) readRepoCmd() []tea.Cmd {
cmds := make([]tea.Cmd, 0)
branchesTaskId := fmt.Sprintf("fetching_branches_%d", time.Now().Unix())
if m.Ctx.RepoPath != nil {
if m.Ctx.RepoPath != "" {
branchesTask := context.Task{
Id: branchesTaskId,
StartText: "Reading local branches",
Expand All @@ -191,7 +191,7 @@ func (m *Model) readRepoCmd() []tea.Cmd {
cmds = append(cmds, bCmd)
}
cmds = append(cmds, func() tea.Msg {
repo, err := git.GetRepo(*m.Ctx.RepoPath)
repo, err := git.GetRepo(m.Ctx.RepoPath)
if err != nil {
return constants.TaskFinishedMsg{TaskId: branchesTaskId, Err: err}
}
Expand All @@ -209,7 +209,7 @@ func (m *Model) readRepoCmd() []tea.Cmd {
func (m *Model) fetchRepoCmd() []tea.Cmd {
cmds := make([]tea.Cmd, 0)
fetchTaskId := fmt.Sprintf("git_fetch_repo_%d", time.Now().Unix())
if m.Ctx.RepoPath == nil {
if m.Ctx.RepoPath == "" {
return []tea.Cmd{}
}
fetchTask := context.Task{
Expand All @@ -221,7 +221,7 @@ func (m *Model) fetchRepoCmd() []tea.Cmd {
}
cmds = append(cmds, m.Ctx.StartTask(fetchTask))
cmds = append(cmds, func() tea.Msg {
repo, err := git.FetchRepo(*m.Ctx.RepoPath)
repo, err := git.FetchRepo(m.Ctx.RepoPath)
if err != nil {
return constants.TaskFinishedMsg{TaskId: fetchTaskId, Err: err}
}
Expand Down Expand Up @@ -251,7 +251,7 @@ func (m *Model) fetchPRsCmd() tea.Cmd {
if limit == nil {
limit = &m.Ctx.Config.Defaults.PrsLimit
}
res, err := data.FetchPullRequests(fmt.Sprintf("author:@me repo:%s", git.GetRepoShortName(*m.Ctx.RepoUrl)), *limit, nil)
res, err := data.FetchPullRequests(fmt.Sprintf("author:@me repo:%s", git.GetRepoShortName(m.Ctx.RepoUrl)), *limit, nil)
if err != nil {
return constants.TaskFinishedMsg{
SectionId: 0,
Expand Down Expand Up @@ -285,7 +285,7 @@ func (m *Model) fetchPRCmd(branch string) []tea.Cmd {
}
startCmd := m.Ctx.StartTask(task)
return []tea.Cmd{startCmd, func() tea.Msg {
res, err := data.FetchPullRequests(fmt.Sprintf("author:@me repo:%s head:%s", git.GetRepoShortName(*m.Ctx.RepoUrl), branch), 1, nil)
res, err := data.FetchPullRequests(fmt.Sprintf("author:@me repo:%s head:%s", git.GetRepoShortName(m.Ctx.RepoUrl), branch), 1, nil)
log.Debug("Fetching PRs", "res", res)
if err != nil {
return constants.TaskFinishedMsg{
Expand Down Expand Up @@ -385,11 +385,11 @@ func (m *Model) deleteBranch() tea.Cmd {
}
startCmd := m.Ctx.StartTask(task)
return tea.Batch(startCmd, func() tea.Msg {
err := gitm.DeleteBranch(*m.Ctx.RepoPath, b.Data.Name, gitm.DeleteBranchOptions{Force: true})
err := gitm.DeleteBranch(m.Ctx.RepoPath, b.Data.Name, gitm.DeleteBranchOptions{Force: true})
if err != nil {
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
}
repo, err := git.GetRepo(*m.Ctx.RepoPath)
repo, err := git.GetRepo(m.Ctx.RepoPath)
if err != nil {
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
}
Expand All @@ -415,11 +415,11 @@ func (m *Model) newBranch(name string) tea.Cmd {
}
startCmd := m.Ctx.StartTask(task)
return tea.Batch(startCmd, func() tea.Msg {
err := gitm.Checkout(*m.Ctx.RepoPath, name, gitm.CheckoutOptions{BaseBranch: m.repo.HeadBranchName})
err := gitm.Checkout(m.Ctx.RepoPath, name, gitm.CheckoutOptions{BaseBranch: m.repo.HeadBranchName})
if err != nil {
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
}
repo, err := git.GetRepo(*m.Ctx.RepoPath)
repo, err := git.GetRepo(m.Ctx.RepoPath)
if err != nil {
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
}
Expand Down
10 changes: 5 additions & 5 deletions ui/components/reposection/reposection.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (m *Model) View() string {
}

return m.Ctx.Styles.Section.ContainerStyle.Render(
lipgloss.JoinVertical(lipgloss.Left, m.SearchBar.View(*m.Ctx), view),
lipgloss.JoinVertical(lipgloss.Left, m.SearchBar.View(m.Ctx), view),
)
}

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

var cmds []tea.Cmd
if m.Ctx.RepoPath != nil {
if m.Ctx.RepoPath != "" {
cmds = append(cmds, m.readRepoCmd()...)
cmds = append(cmds, m.fetchRepoCmd()...)
cmds = append(cmds, m.fetchPRsCmd())
Expand All @@ -479,7 +479,7 @@ func (m *Model) FetchNextPageSectionRows() []tea.Cmd {
return cmds
}

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

t := config.RepoView
Expand All @@ -489,13 +489,13 @@ func FetchAllBranches(ctx context.ProgramContext) (Model, tea.Cmd) {
}
m := NewModel(
0,
&ctx,
ctx,
cfg,
time.Now(),
)
m.refreshId = nextID()

if ctx.RepoPath != nil {
if ctx.RepoPath != "" {
cmds = append(cmds, m.readRepoCmd()...)
cmds = append(cmds, m.fetchRepoCmd()...)
cmds = append(cmds, m.fetchPRsCmd())
Expand Down
2 changes: 1 addition & 1 deletion ui/components/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
return m, cmd
}

func (m Model) View(ctx context.ProgramContext) string {
func (m Model) View(ctx *context.ProgramContext) string {
return lipgloss.NewStyle().
Width(ctx.MainContentWidth - 4).
MaxHeight(3).
Expand Down
Loading

0 comments on commit 51b705a

Please sign in to comment.