singularity-forge/native
Flux Labs 343a43f028 feat: move git operations to Rust via git2 crate (#572)
* feat: move git operations to Rust via git2 crate (#524)

Eliminates ~70 execSync/execFileSync git CLI calls across 15 TypeScript
files by implementing native libgit2 operations in Rust and routing all
consumers through the native-git-bridge.

Rust (native/crates/engine/src/git.rs):
- Added 28 new NAPI functions covering both read and write operations
- Read: git_is_repo, git_has_staged_changes, git_diff_stat,
  git_diff_name_status, git_diff_numstat, git_diff_content,
  git_log_oneline, git_worktree_list, git_branch_list,
  git_branch_list_merged, git_ls_files, git_for_each_ref,
  git_conflict_files, git_batch_info
- Write: git_init, git_add_all, git_add_paths, git_reset_paths,
  git_commit, git_checkout_branch, git_checkout_theirs,
  git_merge_squash, git_merge_abort, git_rebase_abort,
  git_reset_hard, git_branch_delete, git_branch_force_reset,
  git_rm_cached, git_rm_force, git_worktree_add,
  git_worktree_remove, git_worktree_prune, git_revert_commit,
  git_revert_abort, git_update_ref

TypeScript (native-git-bridge.ts):
- Added 35 bridge functions with native-first + execSync fallback
- New types: GitDiffStat, GitNameStatus, GitNumstat, GitLogEntry,
  GitWorktreeEntry, GitBatchInfo, GitMergeResult

Consumer migrations (15 files):
- worktree-manager.ts: removed local runGit/getMainBranch, all ops native
- auto-worktree.ts: merge, checkout, conflict resolution all native
- git-service.ts: smart staging, commits, snapshots all native
- auto.ts, guided-flow.ts: repo init/bootstrap native
- auto-supervisor.ts: working tree detection native
- git-self-heal.ts: merge/rebase abort, reset all native
- doctor.ts: health checks, branch listing, worktree cleanup native
- commands.ts: branch/snapshot cleanup native
- session-forensics.ts: diff stat queries native
- auto-recovery.ts: merge state reconciliation native
- gitignore.ts, undo.ts, worktree-command.ts: remaining ops native

Kept as execSync (by design):
- git push (credential handling too complex for libgit2)
- native-git-bridge.ts fallbacks (graceful degradation)
- runPreMergeCheck (runs arbitrary user commands)

Closes #524

* fix: restore getMainBranch export from worktree-manager

The agent migration removed getMainBranch from worktree-manager.ts but
worktree-command.ts still imports it. Re-add as a thin wrapper around
nativeDetectMainBranch.

* fix: address PR #572 review feedback — security, correctness, error handling

CRITICAL:
- Path traversal protection via validate_path_within_repo() for
  git_rm_force and git_checkout_theirs
- git_branch_delete defaults to safe delete (force=false)

HIGH:
- Replace silent .ok() with proper error propagation in git_commit,
  git_merge_abort, git_rebase_abort, git_rm_force, git_checkout_theirs
- nativeDiffStat fallback parses numeric stats from git output
- nativeBatchInfo fallback counts staged/unstaged from porcelain status

MEDIUM:
- Wire up dead force param in removeWorktree()
- Read MERGE_MSG/SQUASH_MSG when commit message empty
- nativeLsFiles uses gitFileExec without fragile quote wrapping
- Fix operator precedence in git_ls_files
2026-03-15 20:02:10 -06:00
..
.cargo feat: Rust native engine scaffold with grep module 2026-03-13 12:21:09 -06:00
crates feat: move git operations to Rust via git2 crate (#572) 2026-03-15 20:02:10 -06:00
npm 2.15.1 2026-03-15 18:54:45 -06:00
scripts fix: use version ranges for native engine optional dependencies (#286) 2026-03-13 16:20:44 -06:00
.gitignore feat: Rust native engine scaffold with grep module 2026-03-13 12:21:09 -06:00
.npmignore fix: include darwin-arm64 native binary in npm tarball (Phase A hotfix) 2026-03-13 14:33:37 -06:00
Cargo.lock feat(native): add libgit2-backed git read operations for dispatch hotpath (#388) 2026-03-14 13:07:02 -06:00
Cargo.toml feat: Rust native engine scaffold with grep module 2026-03-13 12:21:09 -06:00
README.md feat: Rust native engine scaffold with grep module 2026-03-13 12:21:09 -06:00

GSD Native Engine

Rust N-API addon providing high-performance native modules for GSD.

Architecture

JS (packages/native) -> N-API -> Rust crates
                                  ├── engine/  (N-API bindings, cdylib)
                                  └── grep/    (ripgrep internals, pure Rust lib)

Inspired by Oh My Pi's pi-natives, adapted for GSD's Node.js runtime.

Prerequisites

Build

# Release build (optimized)
npm run build:native

# Debug build (fast compile, no optimizations)
npm run build:native:dev

The build script compiles the Rust code and copies the .node shared library to native/addon/.

Test

# Rust unit tests
cd native && cargo test

# Node.js integration tests
npm run test:native

Modules

grep

Ripgrep-backed regex search using the grep-regex, grep-searcher, and grep-matcher crates.

Functions:

  • search(content, options) — Search in-memory Buffer/Uint8Array content
  • grep(options) — Search files on disk with glob filtering and .gitignore support

TypeScript usage:

import { grep, searchContent } from "@gsd/native";

// Search files
const result = grep({
  pattern: "TODO",
  path: "./src",
  glob: "*.ts",
  ignoreCase: true,
  maxCount: 100,
});

// Search content
const contentResult = searchContent(Buffer.from(fileContent), {
  pattern: "function\\s+\\w+",
  contextBefore: 2,
  contextAfter: 2,
});

Adding New Modules

  1. Create a new crate in native/crates/ (pure Rust library)
  2. Add N-API bindings in native/crates/engine/src/
  3. Add TypeScript wrapper in packages/native/src/
  4. Add the crate to engine/Cargo.toml dependencies