From b0705aa62163032479922c174fbb4df99b7ff68b Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Sun, 5 Jan 2025 00:32:41 +0200 Subject: [PATCH 1/2] getStagedFiles() -> hasStagedFiles() --- src/GitFacade.ts | 4 ++-- src/extension.ts | 4 ++-- test/GitFacade.test.ts | 8 ++++++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/GitFacade.ts b/src/GitFacade.ts index 6f60abc..4943987 100644 --- a/src/GitFacade.ts +++ b/src/GitFacade.ts @@ -15,9 +15,9 @@ export class GitFacade { this.git.cwd(path) } - async getStagedFiles(): Promise { + async hasStagedFiles(): Promise { const diff = await this.git.diff(['--name-only', '--cached']); - return diff.split('\n').filter((line) => line !== '') + return (diff.trim() !== '') } async getCurrentBranch(): Promise { diff --git a/src/extension.ts b/src/extension.ts index 6b56731..dd6c789 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -21,8 +21,8 @@ export const activate = (context: vscode.ExtensionContext): void => { } git.updateWorkingDirectory(workspaceFolder) - const stagedFiles = await git.getStagedFiles() - if (stagedFiles.length === 0) { + const hasStagedFiles = await git.hasStagedFiles() + if (!hasStagedFiles) { vscode.window.showErrorMessage('No staged files') return } diff --git a/test/GitFacade.test.ts b/test/GitFacade.test.ts index 9e52505..763b75c 100644 --- a/test/GitFacade.test.ts +++ b/test/GitFacade.test.ts @@ -67,4 +67,12 @@ describe('GitFacade', () => { expect(commits.map((c) => c.hash.length)).toEqual([expectedLength, expectedLength, expectedLength]) expect(commits.map((c) => c.subject)).toEqual(['subject 3', 'subject 2', 'subject 1']) }) + + it('hasStagedFiles()', async () => { + expect(await facade.hasStagedFiles()).toBe(false) + await modifyFileAndStageChanges('foobar') + expect(await facade.hasStagedFiles()).toBe(true) + await git.commit('-') + expect(await facade.hasStagedFiles()).toBe(false) + }) }) \ No newline at end of file From 1685c4522abeb762f53b16dfdadb67073ad68be9 Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Sun, 5 Jan 2025 00:34:48 +0200 Subject: [PATCH 2/2] rename doesBranchExist(), make private --- src/GitFacade.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/GitFacade.ts b/src/GitFacade.ts index 4943987..1cb0580 100644 --- a/src/GitFacade.ts +++ b/src/GitFacade.ts @@ -28,14 +28,14 @@ export class GitFacade { async getMainBranch(): Promise { const CANDIDATES = ['main', 'master'] for (const candidate of CANDIDATES) { - if (await this.doesBranchExist(candidate)) { + if (await this.hasBranch(candidate)) { return candidate } } throw new Error('No main branch') } - async doesBranchExist(name: BranchName): Promise { + private async hasBranch(name: BranchName): Promise { const result = await this.git.raw(['branch', '-l', name]) return (result.trim() !== '') }