Skip to content

Commit 1774aa6

Browse files
authored
Commit Messages: Fix bug with unstaged files (#3150)
## Description This PR: - Fixes an issue where VS Code would provide `changes` as an empty array for unstaged files - Adds a e2e test to cover this - Adds an item to the Cody feature menu to enable/disable the setting ## Test plan 1. Trigger the Cody commit message with no staged files 2. Check it works! Other: 1. Check that disabling/enabling the feature from the menu works <!-- Required. See https://sourcegraph.com/docs/dev/background-information/testing_principles. -->
1 parent bea7e69 commit 1774aa6

File tree

4 files changed

+66
-6
lines changed

4 files changed

+66
-6
lines changed

vscode/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@
852852
"order": 99,
853853
"type": "boolean",
854854
"default": true,
855-
"markdownDescription": "Adds a button to the SCM input field to generate a relevant commit message for the current changes."
855+
"markdownDescription": "Enable Cody to appear in the Source Control input field, to generate a relevant commit message from your changes."
856856
},
857857
"cody.debug.enable": {
858858
"order": 99,

vscode/src/scm/CommitMessageProvider.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ export class CommitMessageProvider implements VSCodeCommitMessageProvider, vscod
6363
hasV2Event: true,
6464
})
6565
telemetryRecorder.recordEvent('cody.command.generateCommitMessage', 'clicked')
66-
const humanPrompt = await this.getHumanPrompt(changes)
66+
67+
// Filter out any empty changes.
68+
// The Git extension seems to provide empty strings for unstaged changes.
69+
const diffs = changes.filter(diff => diff.trim().length > 0)
70+
71+
const humanPrompt = await this.getHumanPrompt(diffs)
6772
if (!humanPrompt) {
6873
return Promise.reject()
6974
}

vscode/src/services/StatusBar.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ export function createStatusBar(): CodyStatusBar {
158158
c => c.experimentalSymfContext,
159159
false
160160
),
161+
await createFeatureToggle(
162+
'Commit Messages',
163+
'Experimental',
164+
'Enable Cody to appear in the Source Control input field, to generate a relevant commit message from your changes.',
165+
'cody.experimental.commitMessage',
166+
c => c.experimentalCommitMessage
167+
),
161168
{ label: 'settings', kind: vscode.QuickPickItemKind.Separator },
162169
{
163170
label: '$(gear) Cody Extension Settings',

vscode/test/e2e/commit-message.test.ts

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,28 @@ const test = baseTest
2424
await withTempDir(async dir => {
2525
// Initialize a git repository there
2626
await spawn('git', ['init'], { cwd: dir })
27+
await spawn('git', ['config', 'user.name', 'Test User'], {
28+
cwd: dir,
29+
})
30+
await spawn('git', ['config', 'user.email', 'test@example.host'], { cwd: dir })
2731

2832
// Add Cody ignore
2933
await fs.mkdir(path.join(dir, '.cody'), { recursive: true })
3034
await fs.writeFile(path.join(dir, '.cody', 'ignore'), 'ignored.js')
3135

32-
// Add some content
36+
// Add empty files to change later
37+
await Promise.all([
38+
fs.writeFile(path.join(dir, 'index.js'), ''),
39+
fs.writeFile(path.join(dir, 'ignored.js'), ''),
40+
])
41+
42+
// Commit initial files
43+
await spawn('git', ['add', '.'], { cwd: dir })
44+
await spawn('git', ['commit', '-m', 'Initial commit'], {
45+
cwd: dir,
46+
})
47+
48+
// Add some content to try to commit in our tests
3349
await Promise.all([
3450
fs.writeFile(path.join(dir, 'index.js'), '// Hello World'),
3551
fs.writeFile(path.join(dir, 'ignored.js'), '// Ignore me!'),
@@ -44,7 +60,7 @@ test.beforeEach(() => {
4460
mockServer.resetLoggedEvents()
4561
})
4662

47-
test('commit message generation - happy path', async ({ page, sidebar }) => {
63+
test('commit message generation - happy path with staged changes', async ({ page, sidebar }) => {
4864
// Sign into Cody
4965
await sidebarSignin(page, sidebar)
5066

@@ -55,7 +71,7 @@ test('commit message generation - happy path', async ({ page, sidebar }) => {
5571
.click()
5672

5773
// Check the change is showing as a Git change
58-
const gitChange = page.getByLabel('index.js, Untracked')
74+
const gitChange = page.getByLabel('index.js • Modified')
5975
await expect(gitChange).toBeVisible()
6076

6177
// Stage Git change
@@ -80,6 +96,38 @@ test('commit message generation - happy path', async ({ page, sidebar }) => {
8096
).toBeVisible()
8197
})
8298

99+
test('commit message generation - happy path with no staged changes', async ({ page, sidebar }) => {
100+
// Sign into Cody
101+
await sidebarSignin(page, sidebar)
102+
103+
// Open the Source Control view
104+
await page
105+
.getByLabel(/Source Control/)
106+
.nth(2)
107+
.click()
108+
109+
// Check the change is showing as a Git change
110+
const gitChange = page.getByLabel('index.js • Modified')
111+
await expect(gitChange).toBeVisible()
112+
113+
// Activate the Cody commit message feature
114+
const generateCommitMessageCta = await page.getByLabel('Generate Commit Message (Cody)')
115+
expect(generateCommitMessageCta).toBeVisible()
116+
await generateCommitMessageCta.hover()
117+
await generateCommitMessageCta.click()
118+
119+
const expectedEvents = [
120+
'CodyVSCodeExtension:command:generateCommitMessage:clicked',
121+
'CodyVSCodeExtension:command:generateCommitMessage:executed',
122+
]
123+
await assertEvents(mockServer.loggedEvents, expectedEvents)
124+
125+
// Check generated content is displayed in the source control input
126+
await expect(
127+
page.getByLabel('Source Control Input').getByText('hello from the assistant')
128+
).toBeVisible()
129+
})
130+
83131
test('commit message generation - cody ignore', async ({ page, sidebar }) => {
84132
// Sign into Cody
85133
await sidebarSignin(page, sidebar)
@@ -91,7 +139,7 @@ test('commit message generation - cody ignore', async ({ page, sidebar }) => {
91139
.click()
92140

93141
// Check the change is showing as a Git change
94-
const gitChange = page.getByLabel('ignored.js, Untracked')
142+
const gitChange = page.getByLabel('ignored.js • Modified')
95143
await expect(gitChange).toBeVisible()
96144

97145
// Stage Git change

0 commit comments

Comments
 (0)