Skip to content

Create src/git.ts safe git wrapper module to eliminate try/catch chains #175

@xliry

Description

@xliry

Problem

The codebase has 92 try blocks, 95 catch blocks across 2298 lines. Most exist because raw execSync throws on any git failure. Instead of try/catch everywhere, we need a safe abstraction layer.

What to do

Create src/git.ts with these safe wrapper functions that never throw:

Core helper

function gitExec(cmd: string, cwd: string): { ok: boolean; output: string }

Runs execSync, catches internally, returns result object. All other helpers build on this.

State query functions (check before act)

function branchExists(cwd: string, branch: string): boolean
function worktreeExists(cwd: string, path: string): boolean
function isGitRepo(dir: string): boolean  // move from worktree.ts
function hasUncommittedChanges(cwd: string): boolean
function getCurrentBranch(cwd: string): string | null
function hasConflicts(cwd: string): boolean

Safe action functions

function checkout(cwd: string, branch: string): boolean
function deleteBranch(cwd: string, branch: string): boolean  // checks existence first
function pull(cwd: string, remote?: string, branch?: string): { ok: boolean; output: string }
function push(cwd: string, remote?: string): { ok: boolean; output: string }
function merge(cwd: string, branch: string): { ok: boolean; hasConflicts: boolean; output: string }
function stash(cwd: string): boolean  // returns true if something was stashed
function stashPop(cwd: string): boolean
function mergeAbort(cwd: string): void
function rebase(cwd: string, onto: string): { ok: boolean; output: string }
function rebaseAbort(cwd: string): void

Design principles

  • No function throws — all return result objects or booleans
  • Check state before actingdeleteBranch checks branchExists first
  • Log at dim() level when operations fail silently (not /* ignore */)
  • Import dim from ./logging.js for diagnostic output
  • Export everything for use in worktree.ts and process.ts

Files

  • src/git.ts — NEW file

Acceptance

  • All git operations wrapped safely
  • No function throws exceptions
  • Failed operations logged at dim() level
  • Exported and ready for worktree.ts and process.ts refactors

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions