From 9354dbd9f2784c8fe4b092cdc4c0b08e71bb1b43 Mon Sep 17 00:00:00 2001 From: Steve Yegge Date: Tue, 23 Dec 2025 22:37:08 -0800 Subject: [PATCH] Refactor duplicate JSONL-from-git parsing code (bd-y2v) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extract readFromGitRef helper function to eliminate duplicate code for running git show : commands. The helper is now used by: - checkGitForIssues() in autoimport.go - importFromGit() in autoimport.go - readFirstIssueFromGit() in init.go The scanning/parsing logic is intentionally kept separate since each function has different requirements (error handling, SetDefaults, etc). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- cmd/bd/autoimport.go | 26 +++++++++++++++++--------- cmd/bd/init.go | 7 ++----- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/cmd/bd/autoimport.go b/cmd/bd/autoimport.go index 5bd690e6..f69957cb 100644 --- a/cmd/bd/autoimport.go +++ b/cmd/bd/autoimport.go @@ -20,6 +20,20 @@ import ( "gopkg.in/yaml.v3" ) +// readFromGitRef reads file content from a git ref (branch or commit). +// Returns the raw bytes from git show :. +// The filePath is automatically converted to forward slashes for Windows compatibility. +// Returns nil, err if the git command fails (e.g., file not found in ref). +func readFromGitRef(filePath, gitRef string) ([]byte, error) { + gitPath := filepath.ToSlash(filePath) + cmd := exec.Command("git", "show", fmt.Sprintf("%s:%s", gitRef, gitPath)) // #nosec G204 - git command with safe args + output, err := cmd.Output() + if err != nil { + return nil, fmt.Errorf("failed to read from git: %w", err) + } + return output, nil +} + // checkAndAutoImport checks if the database is empty but git has issues. // If so, it automatically imports them and returns true. // Returns false if no import was needed or if import failed. @@ -129,10 +143,7 @@ func checkGitForIssues() (int, string, string) { } for _, relPath := range candidates { - // Use ToSlash for git path compatibility on Windows - gitPath := filepath.ToSlash(relPath) - cmd := exec.Command("git", "show", fmt.Sprintf("%s:%s", gitRef, gitPath)) // #nosec G204 - git command with safe args - output, err := cmd.Output() + output, err := readFromGitRef(relPath, gitRef) if err == nil && len(output) > 0 { lines := bytes.Count(output, []byte("\n")) if lines > 0 { @@ -197,12 +208,9 @@ func getLocalSyncBranch(beadsDir string) string { // importFromGit imports issues from git at the specified ref (bd-0is: supports sync-branch) func importFromGit(ctx context.Context, dbFilePath string, store storage.Storage, jsonlPath, gitRef string) error { - // Get content from git (use ToSlash for Windows compatibility) - gitPath := filepath.ToSlash(jsonlPath) - cmd := exec.Command("git", "show", fmt.Sprintf("%s:%s", gitRef, gitPath)) // #nosec G204 - git command with safe args - jsonlData, err := cmd.Output() + jsonlData, err := readFromGitRef(jsonlPath, gitRef) if err != nil { - return fmt.Errorf("failed to read from git: %w", err) + return err } // Parse JSONL data diff --git a/cmd/bd/init.go b/cmd/bd/init.go index 5725f04e..e02bd070 100644 --- a/cmd/bd/init.go +++ b/cmd/bd/init.go @@ -1333,12 +1333,9 @@ func readFirstIssueFromJSONL(path string) (*types.Issue, error) { // readFirstIssueFromGit reads the first issue from a git ref (bd-0is: supports sync-branch) func readFirstIssueFromGit(jsonlPath, gitRef string) (*types.Issue, error) { - // Get content from git (use ToSlash for Windows compatibility) - gitPath := filepath.ToSlash(jsonlPath) - cmd := exec.Command("git", "show", fmt.Sprintf("%s:%s", gitRef, gitPath)) // #nosec G204 - output, err := cmd.Output() + output, err := readFromGitRef(jsonlPath, gitRef) if err != nil { - return nil, fmt.Errorf("failed to read from git: %w", err) + return nil, err } scanner := bufio.NewScanner(bytes.NewReader(output))