Git Integration
Git Integration
Fixwright manages Git branches and commits automatically when applying fixes. This page covers Git configuration and usage.
Configuration
import { FixWright } from '@korvol/fixwright';
const fixwright = new FixWright({ ai: { apiKey: process.env.ANTHROPIC_API_KEY! },
git: { baseBranch: 'main', fixBranchPrefix: 'fix/stepwright-', commitAuthor: { name: 'Fixwright Bot', email: 'fixwright@example.com', }, },
workDir: '/path/to/repo',});Git Configuration Options
baseBranch
The main branch to create fix branches from:
git: { baseBranch: 'main', // or 'master', 'develop', etc.}Fixwright will:
- Checkout the base branch
- Pull latest changes
- Create a new fix branch
fixBranchPrefix
Prefix for fix branch names:
git: { fixBranchPrefix: 'fix/stepwright-',}Resulting branch names: fix/stepwright-fc-2024-01-15-abc123
commitAuthor
Author for commits (optional):
git: { commitAuthor: { name: 'Fixwright Bot', email: 'fixwright@example.com', },}If not specified, uses the default Git config.
workDir
Working directory for Git operations:
{ workDir: process.cwd(), // Default: current directory}Branch Workflow
1. Branch Creation
When processing a failure case:
fixwright.on('attempt:start', async (attempt) => { // Branch created: fix/stepwright-fc-2024-01-15-abc123 console.log('Working on branch');});Internal flow:
git checkout maingit pull origin maingit checkout -b fix/stepwright-fc-2024-01-15-abc1232. Applying Changes
Claude’s edits are saved to the working directory:
fixwright.on('agent:edit', (editInfo) => { console.log(`Modified: ${editInfo.filePath}`); // File is edited on disk (not committed yet)});3. Committing
After successful validation, changes are committed:
git add <modified-file>git commit -m "fix(stepwright): Update selector for Login button
Fixes: fc-2024-01-15-abc123"4. Pushing
Branch is pushed to remote:
git push -u origin fix/stepwright-fc-2024-01-15-abc123GitManager API
For advanced usage, access the GitManager directly:
import { GitManager } from '@korvol/fixwright';
const git = new GitManager('/path/to/repo', { baseBranch: 'main', fixBranchPrefix: 'fix/stepwright-',});
// Create a fix branchconst result = await git.createFixBranch('fc-abc123');console.log('Branch:', result.output);
// Get current branchconst branch = await git.getCurrentBranch();console.log('On branch:', branch);
// Commit changesawait git.commitFix('tests/login.spec.ts', 'Update selector', 'fc-abc123');
// Push to remoteawait git.pushBranch();GitManager Methods
| Method | Description |
|---|---|
clone(url, branch?) | Clone a repository |
checkout(ref) | Checkout a branch or commit |
createFixBranch(id) | Create a new fix branch |
getCurrentBranch() | Get current branch name |
commitFix(file, message, id) | Stage and commit a file |
pushBranch() | Push current branch |
hasUncommittedChanges() | Check for pending changes |
resetChanges() | Discard uncommitted changes |
getCurrentCommit() | Get current commit hash |
branchExists(name) | Check if branch exists |
Working with Remote Repositories
Cloning Before Fixing
For CI/CD pipelines, clone the repository first:
const git = new GitManager('./work', config);
await git.clone('https://github.com/org/repo', 'main');
const fixwright = new FixWright({ // ... config workDir: './work',});Authentication
For pushing to remotes, configure Git authentication:
# SSH key (recommended)git config --global url."git@github.com:".insteadOf "https://github.com/"
# Or use credential helpergit config --global credential.helper storeOr use HTTPS with token:
git clone https://${GITHUB_TOKEN}@github.com/org/repo.gitHandling Conflicts
Detecting Conflicts
Check for uncommitted changes before starting:
const hasChanges = await git.hasUncommittedChanges();if (hasChanges) { console.warn('Repository has uncommitted changes');}Resolving Conflicts
If a branch already exists:
const exists = await git.branchExists('fix/stepwright-fc-abc123');if (exists) { // Branch from previous attempt await git.checkout('fix/stepwright-fc-abc123');} else { await git.createFixBranch('fc-abc123');}Reverting Failed Fixes
After a failed validation, revert changes:
fixwright.on('attempt:failed', async (attempt) => { // Fixwright automatically reverts console.log('Changes reverted');});
// Or manuallyawait git.resetChanges();Commit Message Format
Default commit message format:
fix(stepwright): <brief description>
<detailed explanation if available>
Fixes: <failure-case-id>Example:
fix(stepwright): Update selector for submit button
The button ID changed from #submit to #submit-btn.Updated the selector to use the new ID.
Fixes: fc-2024-01-15-abc123Best Practices
1. Use a Clean Working Directory
// Ensure clean state before processingif (await git.hasUncommittedChanges()) { await git.resetChanges();}2. Use Dedicated Work Directory
// Don't modify your development copy{ workDir: '/tmp/fixwright-work',}3. Pull Before Creating Branch
The createFixBranch method pulls automatically, but you can verify:
await git.checkout('main');// createFixBranch will pull latestawait git.createFixBranch('fc-abc123');4. Verify Branch After Creation
const result = await git.createFixBranch('fc-abc123');if (!result.success) { console.error('Failed to create branch:', result.error);}Error Handling
const result = await git.commitFix('file.ts', 'message', 'id');
if (!result.success) { console.error('Git error:', result.error);
// Common errors if (result.error?.includes('nothing to commit')) { console.log('No changes to commit'); } if (result.error?.includes('not a git repository')) { console.log('Work directory is not a Git repo'); }}Next Steps
- PR Creation - Create pull requests
- Events and Hooks - Monitor Git operations
- CI/CD Integration - Use in pipelines