From 7c2ea17421496a9437ea0abab5729788e2aef484 Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Wed, 8 Feb 2017 12:37:41 +0100 Subject: [PATCH] git: commit commands --- extensions/git/package.json | 8 +++--- extensions/git/src/commands.ts | 50 ++++++++++++++++++++++------------ extensions/git/src/model.ts | 8 +++++- 3 files changed, 44 insertions(+), 22 deletions(-) diff --git a/extensions/git/package.json b/extensions/git/package.json index c443d4ce063..c3be0f5dc2c 100644 --- a/extensions/git/package.json +++ b/extensions/git/package.json @@ -227,22 +227,22 @@ { "command": "git.commitStaged", "group": "3_commit", - "when": "scmProvider == none" + "when": "scmProvider == git" }, { "command": "git.commitStagedSigned", "group": "3_commit", - "when": "scmProvider == none" + "when": "scmProvider == git" }, { "command": "git.commitAll", "group": "3_commit", - "when": "scmProvider == none" + "when": "scmProvider == git" }, { "command": "git.commitAllSigned", "group": "3_commit", - "when": "scmProvider == none" + "when": "scmProvider == git" }, { "command": "git.undoCommit", diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index 5860d54a507..6eb6fedbd77 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -7,7 +7,7 @@ import { Uri, commands, scm, Disposable, SCMResourceGroup, SCMResource, window, workspace, QuickPickItem, OutputChannel } from 'vscode'; import { IRef, RefType } from './git'; -import { Model, Resource, Status } from './model'; +import { Model, Resource, Status, CommitOptions } from './model'; import * as path from 'path'; import * as nls from 'vscode-nls'; @@ -322,31 +322,39 @@ export class CommandCenter { await this.model.clean(...this.model.workingTreeGroup.resources); } - private async _commit(fn: () => Promise): Promise { - if (this.model.indexGroup.resources.length === 0 && this.model.workingTreeGroup.resources.length === 0) { + private async smartCommit( + getCommitMessage: () => Promise, + opts?: CommitOptions + ): Promise { + if (!opts) { + opts = { all: this.model.indexGroup.resources.length === 0 }; + } + + if ( + // no changes + (this.model.indexGroup.resources.length === 0 && this.model.workingTreeGroup.resources.length === 0) + // or no staged changes and not `all` + || (!opts.all && this.model.indexGroup.resources.length === 0) + ) { window.showInformationMessage(localize('no changes', "There are no changes to commit.")); return false; } - const message = await fn(); + const message = await getCommitMessage(); if (!message) { // TODO@joao: show modal dialog to confirm empty message commit return false; } - const all = this.model.indexGroup.resources.length === 0; - await this.model.commit(message, { all }); + await this.model.commit(message, opts); return true; } - @CommandCenter.Command('git.commit') - @CommandCenter.CatchErrors - async commit(): Promise { + private async commitWithAnyInput(opts?: CommitOptions): Promise { const message = scm.inputBox.value; - - const didCommit = await this._commit(async () => { + const getCommitMessage = async () => { if (message) { return message; } @@ -355,17 +363,25 @@ export class CommandCenter { placeHolder: localize('commit message', "Commit message"), prompt: localize('provide commit message', "Please provide a commit message") }); - }); + }; + + const didCommit = await this.smartCommit(getCommitMessage, opts); if (message && didCommit) { scm.inputBox.value = ''; } } + @CommandCenter.Command('git.commit') + @CommandCenter.CatchErrors + async commit(): Promise { + await this.commitWithAnyInput(); + } + @CommandCenter.Command('git.commitWithInput') @CommandCenter.CatchErrors async commitWithInput(): Promise { - const didCommit = await this._commit(async () => scm.inputBox.value); + const didCommit = await this.smartCommit(async () => scm.inputBox.value); if (didCommit) { scm.inputBox.value = ''; @@ -375,25 +391,25 @@ export class CommandCenter { @CommandCenter.Command('git.commitStaged') @CommandCenter.CatchErrors async commitStaged(): Promise { - await Promise.reject('not implemented'); + await this.commitWithAnyInput({ all: false }); } @CommandCenter.Command('git.commitStagedSigned') @CommandCenter.CatchErrors async commitStagedSigned(): Promise { - await Promise.reject('not implemented'); + await this.commitWithAnyInput({ all: false, signoff: true }); } @CommandCenter.Command('git.commitAll') @CommandCenter.CatchErrors async commitAll(): Promise { - await Promise.reject('not implemented'); + await this.commitWithAnyInput({ all: true }); } @CommandCenter.Command('git.commitAllSigned') @CommandCenter.CatchErrors async commitAllSigned(): Promise { - await Promise.reject('not implemented'); + await this.commitWithAnyInput({ all: true, signoff: true }); } @CommandCenter.Command('git.undoCommit') diff --git a/extensions/git/src/model.ts b/extensions/git/src/model.ts index 349f5e37649..9cf8268ad08 100644 --- a/extensions/git/src/model.ts +++ b/extensions/git/src/model.ts @@ -195,6 +195,12 @@ class OperationsImpl implements Operations { } } +export interface CommitOptions { + all?: boolean; + amend?: boolean; + signoff?: boolean; +} + export class Model { private _onDidChange = new EventEmitter(); @@ -299,7 +305,7 @@ export class Model { } @throttle - async commit(message: string, opts: { all?: boolean, amend?: boolean, signoff?: boolean } = Object.create(null)): Promise { + async commit(message: string, opts: CommitOptions = Object.create(null)): Promise { await this.run(Operation.Commit, async () => { if (opts.all) { await this.repository.add([]); -- GitLab