Skip to content

Commit eb2e5d8

Browse files
authored
Git - Commit action button should use smart commit settings (microsoft#154169)
Consider smart commit settings when rendering the commit action button
1 parent bc0bdc5 commit eb2e5d8

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

extensions/git/src/actionButton.ts

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { Disposable, Event, EventEmitter, SourceControlActionButton, Uri, workspace } from 'vscode';
76
import * as nls from 'vscode-nls';
7+
import { Disposable, Event, EventEmitter, SourceControlActionButton, Uri, workspace } from 'vscode';
8+
import { Branch, Status } from './api/git';
89
import { Repository, Operation } from './repository';
910
import { dispose } from './util';
10-
import { Branch } from './api/git';
1111

1212
const localize = nls.loadMessageBundle();
1313

@@ -16,7 +16,7 @@ interface ActionButtonState {
1616
readonly isCommitInProgress: boolean;
1717
readonly isMergeInProgress: boolean;
1818
readonly isSyncInProgress: boolean;
19-
readonly repositoryHasChanges: boolean;
19+
readonly repositoryHasChangesToCommit: boolean;
2020
}
2121

2222
export class ActionButtonCommand {
@@ -40,19 +40,24 @@ export class ActionButtonCommand {
4040
isCommitInProgress: false,
4141
isMergeInProgress: false,
4242
isSyncInProgress: false,
43-
repositoryHasChanges: false
43+
repositoryHasChangesToCommit: false
4444
};
4545

4646
repository.onDidRunGitStatus(this.onDidRunGitStatus, this, this.disposables);
4747
repository.onDidChangeOperations(this.onDidChangeOperations, this, this.disposables);
4848

4949
const root = Uri.file(repository.root);
5050
this.disposables.push(workspace.onDidChangeConfiguration(e => {
51+
if (e.affectsConfiguration('git.enableSmartCommit', root) ||
52+
e.affectsConfiguration('git.smartCommitChanges', root) ||
53+
e.affectsConfiguration('git.suggestSmartCommit', root)) {
54+
this.onDidChangeSmartCommitSettings();
55+
}
56+
5157
if (e.affectsConfiguration('git.branchProtection', root) ||
5258
e.affectsConfiguration('git.branchProtectionPrompt', root) ||
5359
e.affectsConfiguration('git.postCommitCommand', root) ||
54-
e.affectsConfiguration('git.showActionButton', root)
55-
) {
60+
e.affectsConfiguration('git.showActionButton', root)) {
5661
this._onDidChange.fire();
5762
}
5863
}));
@@ -63,7 +68,7 @@ export class ActionButtonCommand {
6368

6469
let actionButton: SourceControlActionButton | undefined;
6570

66-
if (this.state.repositoryHasChanges) {
71+
if (this.state.repositoryHasChangesToCommit) {
6772
// Commit Changes (enabled)
6873
actionButton = this.getCommitActionButton();
6974
}
@@ -160,7 +165,7 @@ export class ActionButtonCommand {
160165
},
161166
]
162167
],
163-
enabled: this.state.repositoryHasChanges && !this.state.isCommitInProgress && !this.state.isMergeInProgress
168+
enabled: this.state.repositoryHasChangesToCommit && !this.state.isCommitInProgress && !this.state.isMergeInProgress
164169
};
165170
}
166171

@@ -223,19 +228,47 @@ export class ActionButtonCommand {
223228
this.state = { ...this.state, isCommitInProgress, isSyncInProgress };
224229
}
225230

231+
private onDidChangeSmartCommitSettings(): void {
232+
this.state = {
233+
...this.state,
234+
repositoryHasChangesToCommit: this.repositoryHasChangesToCommit()
235+
};
236+
}
237+
226238
private onDidRunGitStatus(): void {
227239
this.state = {
228240
...this.state,
229241
HEAD: this.repository.HEAD,
230-
isMergeInProgress:
231-
this.repository.mergeGroup.resourceStates.length !== 0,
232-
repositoryHasChanges:
233-
this.repository.indexGroup.resourceStates.length !== 0 ||
234-
this.repository.untrackedGroup.resourceStates.length !== 0 ||
235-
this.repository.workingTreeGroup.resourceStates.length !== 0
242+
isMergeInProgress: this.repository.mergeGroup.resourceStates.length !== 0,
243+
repositoryHasChangesToCommit: this.repositoryHasChangesToCommit()
236244
};
237245
}
238246

247+
private repositoryHasChangesToCommit(): boolean {
248+
const config = workspace.getConfiguration('git', Uri.file(this.repository.root));
249+
const enableSmartCommit = config.get<boolean>('enableSmartCommit') === true;
250+
const suggestSmartCommit = config.get<boolean>('suggestSmartCommit') === true;
251+
const smartCommitChanges = config.get<'all' | 'tracked'>('smartCommitChanges', 'all');
252+
253+
const resources = [...this.repository.indexGroup.resourceStates];
254+
255+
if (
256+
// Smart commit enabled (all)
257+
(enableSmartCommit && smartCommitChanges === 'all') ||
258+
// Smart commit disabled, smart suggestion enabled
259+
(!enableSmartCommit && suggestSmartCommit)
260+
) {
261+
resources.push(...this.repository.workingTreeGroup.resourceStates);
262+
}
263+
264+
// Smart commit enabled (tracked only)
265+
if (enableSmartCommit && smartCommitChanges === 'tracked') {
266+
resources.push(...this.repository.workingTreeGroup.resourceStates.filter(r => r.type !== Status.UNTRACKED));
267+
}
268+
269+
return resources.length !== 0;
270+
}
271+
239272
dispose(): void {
240273
this.disposables = dispose(this.disposables);
241274
}

0 commit comments

Comments
 (0)