Auto publish branches before Continue On in git repositories (#171211)

* Support syncing with remote before Continue On

* Auto publish before Continue On in git extension

* Add configuration description
This commit is contained in:
Joyce Er
2023-01-18 03:25:47 -08:00
committed by GitHub
parent 5bbd1f4745
commit e7d34457a5
4 changed files with 82 additions and 0 deletions
+9
View File
@@ -2673,6 +2673,15 @@
"tags": [
"experimental"
]
},
"git.publishBeforeContinueOn": {
"type": "boolean",
"default": true,
"markdownDescription": "%config.publishBeforeContinueOn%",
"scope": "resource",
"tags": [
"experimental"
]
}
}
},
+1
View File
@@ -242,6 +242,7 @@
"config.useIntegratedAskPass": "Controls whether GIT_ASKPASS should be overwritten to use the integrated version.",
"config.mergeEditor": "Open the merge editor for files that are currently under conflict.",
"config.optimisticUpdate": "Controls whether to optimistically update the state of the Source Control view after running git commands.",
"config.publishBeforeContinueOn": "Controls whether to publish unpublished git state when using Continue Working On from a git repository.",
"submenu.explorer": "Git",
"submenu.commit": "Commit",
"submenu.commit.amend": "Amend",
@@ -5,6 +5,7 @@
import * as path from 'path';
import * as vscode from 'vscode';
import { RefType } from './api/git';
import { Model } from './model';
export class GitEditSessionIdentityProvider implements vscode.EditSessionIdentityProvider, vscode.Disposable {
@@ -13,6 +14,12 @@ export class GitEditSessionIdentityProvider implements vscode.EditSessionIdentit
constructor(private model: Model) {
this.providerRegistration = vscode.workspace.registerEditSessionIdentityProvider('file', this);
vscode.workspace.onWillCreateEditSessionIdentity((e) => {
if (vscode.workspace.getConfiguration('git').get('publishBeforeContinueOn')) {
e.waitUntil(this._onWillCreateEditSessionIdentity(e.workspaceFolder, e.token));
}
});
}
dispose() {
@@ -58,6 +65,33 @@ export class GitEditSessionIdentityProvider implements vscode.EditSessionIdentit
return vscode.EditSessionIdentityMatch.Partial;
}
}
private async _onWillCreateEditSessionIdentity(workspaceFolder: vscode.WorkspaceFolder, cancellationToken: vscode.CancellationToken): Promise<void> {
const cancellationPromise = createCancellationPromise(cancellationToken);
await Promise.race([this._doPublish(workspaceFolder), cancellationPromise]);
}
private async _doPublish(workspaceFolder: vscode.WorkspaceFolder) {
await this.model.openRepository(path.dirname(workspaceFolder.uri.fsPath));
const repository = this.model.getRepository(workspaceFolder.uri);
if (!repository) {
return;
}
await repository.status();
// If this branch hasn't been published to the remote yet,
// ensure that it is published before Continue On is invoked
if (!repository.HEAD?.upstream && repository.HEAD?.type === RefType.Head) {
await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: vscode.l10n.t('Publishing branch...')
}, async () => {
await vscode.commands.executeCommand('git.publish');
});
}
}
}
function normalizeEditSessionIdentity(identity: string) {
@@ -73,3 +107,14 @@ function normalizeEditSessionIdentity(identity: string) {
sha
};
}
function createCancellationPromise(cancellationToken: vscode.CancellationToken) {
return new Promise((resolve, _) => {
if (cancellationToken.isCancellationRequested) {
resolve(undefined);
}
cancellationToken.onCancellationRequested(() => {
resolve(undefined);
});
});
}
@@ -8,6 +8,11 @@ declare module 'vscode' {
// https://github.com/microsoft/vscode/issues/157734
export namespace workspace {
/**
* An event that is emitted when an edit session identity is about to be requested.
*/
export const onWillCreateEditSessionIdentity: Event<EditSessionIdentityWillCreateEvent>;
/**
*
* @param scheme The URI scheme that this provider can provide edit session identities for.
@@ -41,4 +46,26 @@ declare module 'vscode' {
Partial = 50,
None = 0
}
export interface EditSessionIdentityWillCreateEvent {
/**
* A cancellation token.
*/
readonly token: CancellationToken;
/**
* The workspace folder to create an edit session identity for.
*/
readonly workspaceFolder: WorkspaceFolder;
/**
* Allows to pause the event until the provided thenable resolves.
*
* *Note:* This function can only be called during event dispatch.
*
* @param thenable A thenable that delays saving.
*/
waitUntil(thenable: Thenable<any>): void;
}
}