Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commit Action button polish #164746

Merged
merged 4 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion extensions/git/src/actionButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'] }),
Expand Down Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion extensions/git/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1795,7 +1795,7 @@ export class CommandCenter {
}

@command('git.commit', { repository: true })
async commit(repository: Repository, postCommitCommand?: string): Promise<void> {
async commit(repository: Repository, postCommitCommand?: string | null): Promise<void> {
await this.commitWithAnyInput(repository, { postCommitCommand });
}

Expand Down
72 changes: 42 additions & 30 deletions extensions/git/src/postCommitCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?
Expand All @@ -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...');
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -119,19 +132,32 @@ export class CommitCommandsCenter {
return commandGroups;
}

async executePostCommitCommand(command: string | undefined): Promise<void> {
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<void> {
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 {
Expand All @@ -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 {
Expand All @@ -168,29 +194,15 @@ export class CommitCommandsCenter {
return postCommitCommandSetting === 'push' || postCommitCommandSetting === 'sync' ? `git.${postCommitCommandSetting}` : undefined;
}

private getPostCommitCommandStringFromStorage(): string | undefined {
if (!this.isRememberPostCommitCommandEnabled()) {
return undefined;
}

return this.globalState.get<string>(this.repository.root);
private getPostCommitCommandStringFromStorage(): string | null | undefined {
return this.globalState.get<string | null>(this.repository.root);
}

private isRememberPostCommitCommandEnabled(): boolean {
const config = workspace.getConfiguration('git', Uri.file(this.repository.root));
return config.get<boolean>('rememberPostCommitCommand') === true;
}

private async savePostCommitCommand(command: string | undefined): Promise<void> {
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);
}
Expand Down
8 changes: 6 additions & 2 deletions extensions/git/src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ export const enum Operation {
Remove = 'Remove',
RevertFiles = 'RevertFiles',
Commit = 'Commit',
PostCommitCommand = 'PostCommitCommand',
Clean = 'Clean',
Branch = 'Branch',
GetBranch = 'GetBranch',
Expand Down Expand Up @@ -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 } : {};
Expand All @@ -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);
}
});
}
}

Expand Down