Fix git push to pass along --force command

This commit is contained in:
DanConwayDev committed 2025-10-30 08:36:19 +00:00
1 parent 926d1cbeba
commit 8f93a1b55a
2 files changed
+109 -3

No files matched your search

+103
View File
@@ -0,0 +1,103 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { GitPushCommand } from './push';
import type { Git } from '../../git';
import type { JSRuntimeFS } from '../../JSRuntime';
describe('GitPushCommand', () => {
let pushCommand: GitPushCommand;
let mockGit: Partial<Git>;
let mockFS: Partial<JSRuntimeFS>;
const mockPwd = '/test-repo';
beforeEach(() => {
// Mock the filesystem
mockFS = {
stat: vi.fn().mockResolvedValue({}),
};
// Mock Git methods
mockGit = {
currentBranch: vi.fn().mockResolvedValue('main'),
listRemotes: vi.fn().mockResolvedValue([]),
push: vi.fn().mockResolvedValue(undefined),
};
pushCommand = new GitPushCommand({
git: mockGit as Git,
fs: mockFS as JSRuntimeFS,
pwd: mockPwd,
});
});
it('should fail when not in a git repository', async () => {
const mockFSNoRepo: Partial<JSRuntimeFS> = {
stat: vi.fn().mockRejectedValue(new Error('ENOENT')),
};
const cmd = new GitPushCommand({
git: mockGit as Git,
fs: mockFSNoRepo as JSRuntimeFS,
pwd: '/non-existent',
});
const result = await cmd.execute(['origin', 'main']);
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('not a git repository');
});
it('should fail when no remote is configured', async () => {
const result = await pushCommand.execute(['origin', 'main']);
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('does not appear to be a git repository');
});
it('should pass force flag to git.push when --force is used', async () => {
// Configure a mock remote
mockGit.listRemotes = vi.fn().mockResolvedValue([
{ remote: 'origin', url: 'https://example.com/repo.git' },
]);
await pushCommand.execute(['origin', 'main', '--force']);
// Verify that git.push was called with force: true
expect(mockGit.push).toHaveBeenCalledWith(
expect.objectContaining({
force: true,
})
);
});
it('should pass force flag to git.push when -f is used', async () => {
// Configure a mock remote
mockGit.listRemotes = vi.fn().mockResolvedValue([
{ remote: 'origin', url: 'https://example.com/repo.git' },
]);
await pushCommand.execute(['origin', 'main', '-f']);
// Verify that git.push was called with force: true
expect(mockGit.push).toHaveBeenCalledWith(
expect.objectContaining({
force: true,
})
);
});
it('should not pass force flag when not specified', async () => {
// Configure a mock remote
mockGit.listRemotes = vi.fn().mockResolvedValue([
{ remote: 'origin', url: 'https://example.com/repo.git' },
]);
await pushCommand.execute(['origin', 'main']);
// Verify that git.push was called with force: false
expect(mockGit.push).toHaveBeenCalledWith(
expect.objectContaining({
force: false,
})
);
});
});
+6 -3
View File
@@ -33,7 +33,7 @@ export class GitPushCommand implements GitSubcommand {
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
}
const { remote, branch } = this.parseArgs(args);
const { remote, branch, options } = this.parseArgs(args);
// Get current branch if not specified
let targetBranch = branch;
@@ -75,6 +75,7 @@ export class GitPushCommand implements GitSubcommand {
remote: remote,
ref: targetBranch,
remoteRef: targetBranch,
force: options.force,
onAuth: (url) => findCredentialsForRepo(url, settings.credentials),
signer: this.signer,
});
@@ -85,12 +86,14 @@ export class GitPushCommand implements GitSubcommand {
if (error instanceof Error) {
if (error.message.includes('authentication')) {
return createErrorResult('fatal: Authentication failed. Please configure git credentials.');
} else if (error.message.includes('rejected')) {
return createErrorResult(`error: failed to push some refs to '${remoteUrl}'\nhint: Updates were rejected because the remote contains work that you do not have locally.`);
} else if (error.message.includes('rejected') || error.message.includes('Updates were rejected')) {
return createErrorResult(`error: failed to push some refs to '${remoteUrl}'\nhint: ${error.message}`);
} else if (error.message.includes('network')) {
return createErrorResult('fatal: unable to access remote repository. Please check your network connection.');
} else if (error.message.includes('Nostr signer is required')) {
return createErrorResult('fatal: Nostr signer is required for pushing to Nostr repositories. Please log in with a Nostr account.');
} else if (error.message.includes('Everything up-to-date')) {
return createSuccessResult('Everything up-to-date\n');
}
}