Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/GitFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export class GitFacade {
this.git.cwd(path)
}

async getStagedFiles(): Promise<string[]> {
async hasStagedFiles(): Promise<boolean> {
const diff = await this.git.diff(['--name-only', '--cached']);
return diff.split('\n').filter((line) => line !== '')
return (diff.trim() !== '')
}

async getCurrentBranch(): Promise<BranchName> {
Expand All @@ -28,14 +28,14 @@ export class GitFacade {
async getMainBranch(): Promise<BranchName> {
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<boolean> {
private async hasBranch(name: BranchName): Promise<boolean> {
const result = await this.git.raw(['branch', '-l', name])
return (result.trim() !== '')
}
Expand Down
4 changes: 2 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions test/GitFacade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
Loading