diff --git a/extensions/git/src/actionButton.ts b/extensions/git/src/actionButton.ts index ce553a147213f..aa49d179c8a4b 100644 --- a/extensions/git/src/actionButton.ts +++ b/extensions/git/src/actionButton.ts @@ -137,10 +137,13 @@ export class ActionButtonCommand { // Branch does have an upstream, commit/merge/rebase is in progress, or the button is disabled if (this.state.HEAD?.upstream || this.state.isCommitInProgress || this.state.isMergeInProgress || this.state.isRebaseInProgress || !showActionButton.publish) { return undefined; } + // Button icon + const icon = this.state.isSyncInProgress ? '$(sync~spin)' : '$(cloud-upload)'; + return { command: { command: 'git.publish', - title: l10n.t({ message: '{0} Publish Branch', args: ['$(cloud-upload)'], comment: ['{Locked="Branch"}', 'Do not translate "Branch" as it is a git term'] }), + title: l10n.t({ message: '{0} Publish Branch', args: [icon], comment: ['{Locked="Branch"}', 'Do not translate "Branch" as it is a git term'] }), tooltip: this.state.isSyncInProgress ? l10n.t({ message: 'Publishing Branch...', comment: ['{Locked="Branch"}', 'Do not translate "Branch" as it is a git term'] }) : l10n.t({ message: 'Publish Branch', comment: ['{Locked="Branch"}', 'Do not translate "Branch" as it is a git term'] }), @@ -179,6 +182,7 @@ export class ActionButtonCommand { private onDidChangeOperations(): void { const isCommitInProgress = this.repository.operations.isRunning(Operation.Commit) || + this.repository.operations.isRunning(Operation.PostCommitCommand) || this.repository.operations.isRunning(Operation.RebaseContinue); const isSyncInProgress = diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index bc5d4ed162ade..d96e018eee8d7 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -1795,7 +1795,7 @@ export class CommandCenter { } @command('git.commit', { repository: true }) - async commit(repository: Repository, postCommitCommand?: string): Promise { + async commit(repository: Repository, postCommitCommand?: string | null): Promise { await this.commitWithAnyInput(repository, { postCommitCommand }); } diff --git a/extensions/git/src/postCommitCommands.ts b/extensions/git/src/postCommitCommands.ts index 02ce108139c05..a335040e94c0f 100644 --- a/extensions/git/src/postCommitCommands.ts +++ b/extensions/git/src/postCommitCommands.ts @@ -27,7 +27,9 @@ export class GitPostCommitCommandsProvider implements PostCommitCommandsProvider const alwaysCommitToNewBranch = isBranchProtected && branchProtectionPrompt === 'alwaysCommitToNewBranch'; // Icon - const icon = alwaysPrompt ? '$(lock)' : alwaysCommitToNewBranch ? '$(git-branch)' : undefined; + const repository = apiRepository.repository; + const isCommitInProgress = repository.operations.isRunning(Operation.Commit) || repository.operations.isRunning(Operation.PostCommitCommand); + const icon = isCommitInProgress ? '$(sync~spin)' : alwaysPrompt ? '$(lock)' : alwaysCommitToNewBranch ? '$(git-branch)' : undefined; // Tooltip (default) let pushCommandTooltip = !alwaysCommitToNewBranch ? @@ -39,7 +41,7 @@ export class GitPostCommitCommandsProvider implements PostCommitCommandsProvider l10n.t('Commit to New Branch & Synchronize Changes'); // Tooltip (in progress) - if (apiRepository.repository.operations.isRunning(Operation.Commit)) { + if (isCommitInProgress) { pushCommandTooltip = !alwaysCommitToNewBranch ? l10n.t('Committing & Pushing Changes...') : l10n.t('Committing to New Branch & Pushing Changes...'); @@ -71,6 +73,17 @@ export class CommitCommandsCenter { private disposables: Disposable[] = []; + set postCommitCommand(command: string | null | undefined) { + if (command === undefined) { + // Commit WAS NOT initiated using the action button + // so there is no need to store the post-commit command + return; + } + + this.globalState.update(this.repository.root, command) + .then(() => this._onDidChange.fire()); + } + constructor( private readonly globalState: Memento, private readonly repository: Repository, @@ -119,19 +132,32 @@ export class CommitCommandsCenter { return commandGroups; } - async executePostCommitCommand(command: string | undefined): Promise { - if (command === undefined) { - // Commit WAS NOT initiated using the action button (ex: keybinding, toolbar action, - // command palette) so we have to honour the default post commit command (memento/setting). - const primaryCommand = this.getPrimaryCommand(); - command = primaryCommand.arguments?.length === 2 ? primaryCommand.arguments[1] : ''; - } + async executePostCommitCommand(command: string | null | undefined): Promise { + try { + if (command === null) { + // No post-commit command + return; + } - if (command?.length) { - await commands.executeCommand(command, new ApiRepository(this.repository)); - } + if (command === undefined) { + // Commit WAS NOT initiated using the action button (ex: keybinding, toolbar action, + // command palette) so we have to honour the default post commit command (memento/setting). + const primaryCommand = this.getPrimaryCommand(); + command = primaryCommand.arguments?.length === 2 ? primaryCommand.arguments[1] : null; + } - await this.savePostCommitCommand(command); + if (command !== null) { + await commands.executeCommand(command!.toString(), new ApiRepository(this.repository)); + } + } catch (err) { + throw err; + } + finally { + if (!this.isRememberPostCommitCommandEnabled()) { + await this.globalState.update(this.repository.root, undefined); + this._onDidChange.fire(); + } + } } private getCommitCommand(): Command { @@ -158,7 +184,7 @@ export class CommitCommandsCenter { l10n.t('Committing Changes to New Branch...'); } - return { command: 'git.commit', title: l10n.t('{0} Commit', icon ?? '$(check)'), tooltip, arguments: [this.repository.sourceControl, ''] }; + return { command: 'git.commit', title: l10n.t('{0} Commit', icon ?? '$(check)'), tooltip, arguments: [this.repository.sourceControl, null] }; } private getPostCommitCommandStringFromSetting(): string | undefined { @@ -168,12 +194,8 @@ export class CommitCommandsCenter { return postCommitCommandSetting === 'push' || postCommitCommandSetting === 'sync' ? `git.${postCommitCommandSetting}` : undefined; } - private getPostCommitCommandStringFromStorage(): string | undefined { - if (!this.isRememberPostCommitCommandEnabled()) { - return undefined; - } - - return this.globalState.get(this.repository.root); + private getPostCommitCommandStringFromStorage(): string | null | undefined { + return this.globalState.get(this.repository.root); } private isRememberPostCommitCommandEnabled(): boolean { @@ -181,16 +203,6 @@ export class CommitCommandsCenter { return config.get('rememberPostCommitCommand') === true; } - private async savePostCommitCommand(command: string | undefined): Promise { - if (!this.isRememberPostCommitCommandEnabled()) { - return; - } - - command = command !== '' ? command : undefined; - await this.globalState.update(this.repository.root, command); - this._onDidChange.fire(); - } - dispose(): void { this.disposables = dispose(this.disposables); } diff --git a/extensions/git/src/repository.ts b/extensions/git/src/repository.ts index 08d538ecc7469..b937fcdccaa5e 100644 --- a/extensions/git/src/repository.ts +++ b/extensions/git/src/repository.ts @@ -311,6 +311,7 @@ export const enum Operation { Remove = 'Remove', RevertFiles = 'RevertFiles', Commit = 'Commit', + PostCommitCommand = 'PostCommitCommand', Clean = 'Clean', Branch = 'Branch', GetBranch = 'GetBranch', @@ -1261,6 +1262,9 @@ export class Repository implements Disposable { this.closeDiffEditors(indexResources, workingGroupResources); }); } else { + // Set post-commit command to render the correct action button + this.commitCommandCenter.postCommitCommand = opts.postCommitCommand; + await this.run(Operation.Commit, async () => { if (opts.all) { const addOpts = opts.all === 'tracked' ? { update: true } : {}; @@ -1279,9 +1283,9 @@ export class Repository implements Disposable { }); // Execute post-commit command - if (opts.postCommitCommand !== null) { + await this.run(Operation.PostCommitCommand, async () => { await this.commitCommandCenter.executePostCommitCommand(opts.postCommitCommand); - } + }); } }