diff --git a/extensions/git/package.json b/extensions/git/package.json index aba696781b6..be91d5e02d4 100644 --- a/extensions/git/package.json +++ b/extensions/git/package.json @@ -1254,6 +1254,12 @@ "default": false, "description": "%config.alwaysShowStagedChangesResourceGroup%" }, + "git.onlyTrackedFilesCanBeAutoStaged": { + "type": "boolean", + "scope": "resource", + "default": false, + "description": "%config.onlyTrackedFilesCanBeAutoStaged%" + }, "git.alwaysSignOff": { "type": "boolean", "scope": "resource", diff --git a/extensions/git/package.nls.json b/extensions/git/package.nls.json index cddbdad605e..9048bda6bb3 100644 --- a/extensions/git/package.nls.json +++ b/extensions/git/package.nls.json @@ -109,6 +109,7 @@ "config.detectSubmodules": "Controls whether to automatically detect git submodules.", "config.detectSubmodulesLimit": "Controls the limit of git submodules detected.", "config.alwaysShowStagedChangesResourceGroup": "Always show the Staged Changes resource group.", + "config.onlyTrackedFilesCanBeAutoStaged": "Only tracked files can be auto staged", "config.alwaysSignOff": "Controls the signoff flag for all commits.", "config.ignoredRepositories": "List of git repositories to ignore.", "config.scanRepositories": "List of paths to search for git repositories in.", diff --git a/extensions/git/src/repository.ts b/extensions/git/src/repository.ts index a4b37bebd5e..e39e50e7dcb 100644 --- a/extensions/git/src/repository.ts +++ b/extensions/git/src/repository.ts @@ -907,10 +907,22 @@ export class Repository implements Disposable { } async commit(message: string, opts: CommitOptions = Object.create(null)): Promise { + const config = workspace.getConfiguration('git'); + const onlyTrackStagedFile = config.get('onlyTrackedFilesCanBeAutoStaged'); + if (this.rebaseCommit) { await this.run(Operation.RebaseContinue, async () => { if (opts.all) { - await this.repository.add([]); + if (onlyTrackStagedFile) { + const unstageFiles = await this.trackedUnstagedFiles(); + if (unstageFiles.length === 0) { + window.showInformationMessage(localize('no changes', "There are no changes to commit.")); + return false; + } + await this.repository.add(unstageFiles); + } else { + await this.repository.add([]); + } } await this.repository.rebaseContinue(); @@ -918,7 +930,16 @@ export class Repository implements Disposable { } else { await this.run(Operation.Commit, async () => { if (opts.all) { - await this.repository.add([]); + if (onlyTrackStagedFile) { + const unstageFiles = await this.trackedUnstagedFiles(); + if (unstageFiles.length === 0) { + window.showInformationMessage(localize('no changes', "There are no changes to commit.")); + return false; + } + await this.repository.add(unstageFiles); + } else { + await this.repository.add([]); + } } await this.repository.commit(message, opts); @@ -1580,6 +1601,12 @@ export class Repository implements Disposable { this.eventuallyUpdateWhenIdleAndWait(); } + private async trackedUnstagedFiles(): Promise { + const rawChangedFiles = await this.repository.run(['ls-files', '.', '-m']); + const parsedFiles = rawChangedFiles.stdout.split('\n').filter(l => !!l); + return parsedFiles; + } + @debounce(1000) private eventuallyUpdateWhenIdleAndWait(): void { this.updateWhenIdleAndWait();