From 4935612fe6f54dd3f37cc2fd7a280296d3a93989 Mon Sep 17 00:00:00 2001 From: ipmsteven Date: Thu, 14 Mar 2019 23:23:11 -0700 Subject: [PATCH 1/3] introduce onlyTrackedFilesCanBeAutoStaged feature flag --- extensions/git/package.json | 6 ++++++ extensions/git/package.nls.json | 1 + 2 files changed, 7 insertions(+) diff --git a/extensions/git/package.json b/extensions/git/package.json index b6f31076e76..33c964bcba8 100644 --- a/extensions/git/package.json +++ b/extensions/git/package.json @@ -1222,6 +1222,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 21ac86d8fbd..1c1f2552168 100644 --- a/extensions/git/package.nls.json +++ b/extensions/git/package.nls.json @@ -104,6 +104,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.", From 49e0e2607cb396f57d478d4a03ac6581081270ab Mon Sep 17 00:00:00 2001 From: ipmsteven Date: Thu, 14 Mar 2019 23:48:24 -0700 Subject: [PATCH 2/3] add logic to skip staging untracked files when onlyTrackedFilesCanBeAutoStaged is on --- extensions/git/src/repository.ts | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/extensions/git/src/repository.ts b/extensions/git/src/repository.ts index 2fac9e2a39f..e3d8ec3ccab 100644 --- a/extensions/git/src/repository.ts +++ b/extensions/git/src/repository.ts @@ -801,10 +801,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(); @@ -812,7 +824,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); @@ -1449,6 +1470,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(); From 181759fe6ac34627c6c219a5750b4bfaf16c41e7 Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Thu, 8 Aug 2019 11:55:13 +0200 Subject: [PATCH 3/3] add git.smartCommitChanges --- extensions/git/package.json | 20 +++++++++++++------ extensions/git/package.nls.json | 4 +++- extensions/git/src/commands.ts | 6 +++++- extensions/git/src/git.ts | 14 ++++++++++--- extensions/git/src/repository.ts | 34 +++++--------------------------- 5 files changed, 38 insertions(+), 40 deletions(-) diff --git a/extensions/git/package.json b/extensions/git/package.json index be91d5e02d4..6a0e20b3eb3 100644 --- a/extensions/git/package.json +++ b/extensions/git/package.json @@ -1148,6 +1148,20 @@ "description": "%config.enableSmartCommit%", "default": false }, + "git.smartCommitChanges": { + "type": "string", + "enum": [ + "all", + "tracked" + ], + "enumDescriptions": [ + "%config.smartCommitChanges.all%", + "%config.smartCommitChanges.tracked%" + ], + "scope": "resource", + "description": "%config.smartCommitChanges%", + "default": "all" + }, "git.suggestSmartCommit": { "type": "boolean", "scope": "resource", @@ -1254,12 +1268,6 @@ "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 9048bda6bb3..151b62d4458 100644 --- a/extensions/git/package.nls.json +++ b/extensions/git/package.nls.json @@ -89,6 +89,9 @@ "config.ignoreLimitWarning": "Ignores the warning when there are too many changes in a repository.", "config.defaultCloneDirectory": "The default location to clone a git repository.", "config.enableSmartCommit": "Commit all changes when there are no staged changes.", + "config.smartCommitChanges": "Control which changes are automatically staged by Smart Commit.", + "config.smartCommitChanges.all": "Automatically stage all changes.", + "config.smartCommitChanges.tracked": "Automatically staged tracked changes only.", "config.suggestSmartCommit": "Suggests to enable smart commit (commit all changes when there are no staged changes).", "config.enableCommitSigning": "Enables commit signing with GPG.", "config.discardAllScope": "Controls what changes are discarded by the `Discard all changes` command. `all` discards all changes. `tracked` discards only tracked files. `prompt` shows a prompt dialog every time the action is run.", @@ -109,7 +112,6 @@ "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/commands.ts b/extensions/git/src/commands.ts index 39b48f2c5e9..036f152ee4d 100755 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -1275,8 +1275,8 @@ export class CommandCenter { // no changes, and the user has not configured to commit all in this case if (!noUnstagedChanges && noStagedChanges && !enableSmartCommit) { - const suggestSmartCommit = config.get('suggestSmartCommit') === true; + if (!suggestSmartCommit) { return false; } @@ -1330,6 +1330,10 @@ export class CommandCenter { return false; } + if (opts.all && config.get<'all' | 'tracked'>('smartCommitChanges') === 'tracked') { + opts.all = 'tracked'; + } + await repository.commit(message, opts); const postCommitCommand = config.get<'none' | 'push' | 'sync'>('postCommitCommand'); diff --git a/extensions/git/src/git.ts b/extensions/git/src/git.ts index b306cab1255..243bf2471c6 100644 --- a/extensions/git/src/git.ts +++ b/extensions/git/src/git.ts @@ -642,7 +642,7 @@ export function parseLsFiles(raw: string): LsFilesElement[] { } export interface CommitOptions { - all?: boolean; + all?: boolean | 'tracked'; amend?: boolean; signoff?: boolean; signCommit?: boolean; @@ -1081,8 +1081,16 @@ export class Repository { return result.stdout.trim(); } - async add(paths: string[]): Promise { - const args = ['add', '-A', '--']; + async add(paths: string[], opts?: { update?: boolean }): Promise { + const args = ['add']; + + if (opts && opts.update) { + args.push('-u'); + } else { + args.push('-A'); + } + + args.push('--'); if (paths && paths.length) { args.push.apply(args, paths); diff --git a/extensions/git/src/repository.ts b/extensions/git/src/repository.ts index e39e50e7dcb..7320c08eaea 100644 --- a/extensions/git/src/repository.ts +++ b/extensions/git/src/repository.ts @@ -907,22 +907,11 @@ 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) { - 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([]); - } + const addOpts = opts.all === 'tracked' ? { update: true } : {}; + await this.repository.add([], addOpts); } await this.repository.rebaseContinue(); @@ -930,18 +919,11 @@ export class Repository implements Disposable { } else { await this.run(Operation.Commit, async () => { if (opts.all) { - 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([]); - } + const addOpts = opts.all === 'tracked' ? { update: true } : {}; + await this.repository.add([], addOpts); } + delete opts.all; await this.repository.commit(message, opts); }); } @@ -1601,12 +1583,6 @@ 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();