Overview
gitsw is built as a Rust CLI application with a modular architecture that separates concerns into distinct modules. The application usesgit2 for Git operations and maintains its own state in .git/git-switch.json.
Module Structure
The codebase is organized into four core modules defined insrc/lib.rs:1:
git Module
Location:src/git.rs
Provides Git repository operations through the GitRepo wrapper around libgit2:
- Repository operations: Open, get current branch, check branch existence
- Branch management: Create, delete, switch, list branches, track remote branches
- Stash operations: Save, apply, drop, list stashes with OID tracking
- Change detection: Check for uncommitted changes, generate change summaries
- Remote operations: Fetch, pull with fast-forward support
- Tree manipulation: Checkout with safe/force modes, discard changes
GitRepo: Main wrapper struct containinggit2::RepositoryStashInfo: Stash metadata (index, message, OID)
hooks Module
Location:src/hooks.rs
Handles package manager detection and lock file change tracking:
- Package manager detection: Automatically detects npm, yarn, or pnpm based on lock files
- Lock file hashing: Computes SHA256 hashes to detect changes
- Install execution: Runs appropriate install commands for detected package managers
PackageManager: Enum for Npm, Yarn, Pnpm with associated lock file paths and install commands
detect_package_manager(): Checks for lock files in priority order (pnpm > yarn > npm)get_lock_file_hash(): Returns package manager and file hashrun_install(): Spawns install process with inherited stdio
prompt Module
Location:src/prompt.rs
Provides interactive user prompts using dialoguer:
- Branch selection: Fuzzy search picker showing recent branches first
- Conflict resolution: Prompts for stash/discard/abort decisions
- Install prompts: Ask before running package manager installs
- Confirmation dialogs: Confirm destructive operations (delete, discard)
StashAction: Enum for stash conflict resolution (Stash, Discard, Abort)UnstashAction: Enum for unstash conflict resolution (Apply, Skip, Abort)
select_branch(): Interactive fuzzy search for branches sorted by recencyprompt_stash_conflict(): Handle uncommitted changes on switchprompt_unstash_conflict(): Handle stash application conflictsprompt_install(): Confirm package install
state Module
Location:src/state.rs
Manages persistent state in .git/git-switch.json:
- State persistence: Load and save state to Git directory
- Branch tracking: Store per-branch metadata (stash IDs, lock hashes, timestamps)
- Recency tracking: Maintain last visited timestamps for branch sorting
BranchState: Per-branch state with stash_id, lock_file_hash, last_visitedStateData: Root state object containing branches HashMapStateManager: State operations and persistence layer
.git/git-switch.json
Component Interaction
Branch Switch Flow
State Management
State is stored as JSON in.git/git-switch.json with the structure: