feat(v030): GitClient — fetch/rebaseOnto/rebaseAbort/hasUncommittedChanges/listConflicts

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
altair823
2026-05-10 03:23:00 +09:00
parent 662abdb508
commit dba64c546f
2 changed files with 74 additions and 0 deletions

View File

@@ -89,4 +89,27 @@ export class GitClient {
if (r.exitCode !== 0) throw new Error(`git rev-parse failed: ${r.stderr}`);
return r.stdout.trim();
}
async fetch(remote: string = 'origin'): Promise<GitExecResult> {
return this.run(['fetch', remote]);
}
async rebaseOnto(ref: string): Promise<GitExecResult> {
return this.run(['rebase', ref]);
}
async rebaseAbort(): Promise<GitExecResult> {
return this.run(['rebase', '--abort']);
}
async hasUncommittedChanges(): Promise<boolean> {
const r = await this.run(['status', '--porcelain']);
return r.stdout.trim().length > 0;
}
/** rebase conflict 시 conflict 마킹된 파일 list. `git diff --name-only --diff-filter=U`. */
async listConflicts(): Promise<string[]> {
const r = await this.run(['diff', '--name-only', '--diff-filter=U']);
return r.stdout.split('\n').map((s) => s.trim()).filter((s) => s.length > 0);
}
}

View File

@@ -0,0 +1,51 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { GitClient } from '../../src/main/services/GitClient.js';
describe('GitClient — fetch / rebase / conflict 메서드', () => {
let client: GitClient;
let runSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
client = new GitClient('/tmp/sync');
runSpy = vi.spyOn(client, 'run');
});
it('fetch — git fetch origin 호출', async () => {
runSpy.mockResolvedValueOnce({ stdout: '', stderr: '', exitCode: 0 });
const r = await client.fetch();
expect(runSpy).toHaveBeenCalledWith(['fetch', 'origin']);
expect(r.exitCode).toBe(0);
});
it('rebaseOnto — git rebase origin/main', async () => {
runSpy.mockResolvedValueOnce({ stdout: '', stderr: '', exitCode: 0 });
const r = await client.rebaseOnto('origin/main');
expect(runSpy).toHaveBeenCalledWith(['rebase', 'origin/main']);
expect(r.exitCode).toBe(0);
});
it('rebaseAbort — git rebase --abort', async () => {
runSpy.mockResolvedValueOnce({ stdout: '', stderr: '', exitCode: 0 });
await client.rebaseAbort();
expect(runSpy).toHaveBeenCalledWith(['rebase', '--abort']);
});
it('hasUncommittedChanges — git status --porcelain 의 출력 있으면 true', async () => {
runSpy.mockResolvedValueOnce({ stdout: ' M notes/abc.md\n', stderr: '', exitCode: 0 });
expect(await client.hasUncommittedChanges()).toBe(true);
runSpy.mockResolvedValueOnce({ stdout: '', stderr: '', exitCode: 0 });
expect(await client.hasUncommittedChanges()).toBe(false);
});
it('listConflicts — git diff --name-only --diff-filter=U 결과 파싱', async () => {
runSpy.mockResolvedValueOnce({
stdout: 'notes/aaa.md\nnotes/bbb.md\n',
stderr: '',
exitCode: 0
});
const r = await client.listConflicts();
expect(runSpy).toHaveBeenCalledWith(['diff', '--name-only', '--diff-filter=U']);
expect(r).toEqual(['notes/aaa.md', 'notes/bbb.md']);
});
});