Skip to content

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:

  1. Checkout the base branch
  2. Pull latest changes
  3. 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:

Terminal window
git checkout main
git pull origin main
git checkout -b fix/stepwright-fc-2024-01-15-abc123

2. 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:

Terminal window
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:

Terminal window
git push -u origin fix/stepwright-fc-2024-01-15-abc123

GitManager 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 branch
const result = await git.createFixBranch('fc-abc123');
console.log('Branch:', result.output);
// Get current branch
const branch = await git.getCurrentBranch();
console.log('On branch:', branch);
// Commit changes
await git.commitFix('tests/login.spec.ts', 'Update selector', 'fc-abc123');
// Push to remote
await git.pushBranch();

GitManager Methods

MethodDescription
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:

Terminal window
# SSH key (recommended)
git config --global url."git@github.com:".insteadOf "https://github.com/"
# Or use credential helper
git config --global credential.helper store

Or use HTTPS with token:

Terminal window
git clone https://${GITHUB_TOKEN}@github.com/org/repo.git

Handling 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 manually
await 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-abc123

Best Practices

1. Use a Clean Working Directory

// Ensure clean state before processing
if (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 latest
await 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