Skip to content

Commit 8ff93b9

Browse files
committed
git: config.githubAuthentication
1 parent 54d8261 commit 8ff93b9

4 files changed

Lines changed: 52 additions & 7 deletions

File tree

extensions/git/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,12 @@
16711671
"scope": "resource",
16721672
"default": true,
16731673
"description": "%config.terminalAuthentication%"
1674+
},
1675+
"git.githubAuthentication": {
1676+
"type": "boolean",
1677+
"scope": "resource",
1678+
"default": true,
1679+
"description": "%config.githubAuthentication%"
16741680
}
16751681
}
16761682
},

extensions/git/package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
"config.untrackedChanges.hidden": "Untracked changes are hidden and excluded from several actions.",
146146
"config.showCommitInput": "Controls whether to show the commit input in the Git source control panel.",
147147
"config.terminalAuthentication": "Controls whether to enable VS Code to be the authentication handler for git processes spawned in the integrated terminal.",
148+
"config.githubAuthentication": "Controls whether to enable automatic GitHub authentication for git commands within VS Code.",
148149
"colors.added": "Color for added resources.",
149150
"colors.modified": "Color for modified resources.",
150151
"colors.deleted": "Color for deleted resources.",

extensions/git/src/github.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import * as vscode from 'vscode';
76
import { CredentialsProvider, Credentials } from './api/git';
7+
import { IDisposable, filterEvent, EmptyDisposable } from './util';
8+
import { workspace, Uri, AuthenticationSession, authentication } from 'vscode';
9+
import { Askpass } from './askpass';
810

911
export class GitHubCredentialProvider implements CredentialsProvider {
1012

11-
async getCredentials(host: vscode.Uri): Promise<Credentials | undefined> {
13+
async getCredentials(host: Uri): Promise<Credentials | undefined> {
1214
if (!/github\.com/i.test(host.authority)) {
1315
return;
1416
}
@@ -17,13 +19,48 @@ export class GitHubCredentialProvider implements CredentialsProvider {
1719
return { username: session.account.id, password: await session.getAccessToken() };
1820
}
1921

20-
private async getSession(): Promise<vscode.AuthenticationSession> {
21-
const authenticationSessions = await vscode.authentication.getSessions('github', ['repo']);
22+
private async getSession(): Promise<AuthenticationSession> {
23+
const authenticationSessions = await authentication.getSessions('github', ['repo']);
2224

2325
if (authenticationSessions.length) {
2426
return await authenticationSessions[0];
2527
} else {
26-
return await vscode.authentication.login('github', ['repo']);
28+
return await authentication.login('github', ['repo']);
2729
}
2830
}
2931
}
32+
33+
export class GithubCredentialProviderManager {
34+
35+
private providerDisposable: IDisposable = EmptyDisposable;
36+
private readonly disposable: IDisposable;
37+
38+
private _enabled = false;
39+
private set enabled(enabled: boolean) {
40+
if (this._enabled === enabled) {
41+
return;
42+
}
43+
44+
this._enabled = enabled;
45+
46+
if (enabled) {
47+
this.providerDisposable = this.askpass.registerCredentialsProvider(new GitHubCredentialProvider());
48+
} else {
49+
this.providerDisposable.dispose();
50+
}
51+
}
52+
53+
constructor(private readonly askpass: Askpass) {
54+
this.disposable = filterEvent(workspace.onDidChangeConfiguration, e => e.affectsConfiguration('git'))(this.refresh, this);
55+
this.refresh();
56+
}
57+
58+
private refresh(): void {
59+
this.enabled = workspace.getConfiguration('git', null).get('githubAuthentication', true);
60+
}
61+
62+
dispose(): void {
63+
this.enabled = false;
64+
this.disposable.dispose();
65+
}
66+
}

extensions/git/src/main.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import * as path from 'path';
2222
import * as fs from 'fs';
2323
import { GitTimelineProvider } from './timelineProvider';
2424
import { registerAPICommands } from './api/api1';
25-
import { GitHubCredentialProvider } from './github';
25+
import { GitHubCredentialProvider, GithubCredentialProviderManager } from './github';
2626
import { TerminalEnvironmentManager } from './terminal';
2727

2828
const deactivateTasks: { (): Promise<any>; }[] = [];
@@ -44,7 +44,8 @@ async function createModel(context: ExtensionContext, outputChannel: OutputChann
4444
const terminalEnvironmentManager = new TerminalEnvironmentManager(context, env);
4545
disposables.push(terminalEnvironmentManager);
4646

47-
context.subscriptions.push(askpass.registerCredentialsProvider(new GitHubCredentialProvider()));
47+
const githubCredentialProviderManager = new GithubCredentialProviderManager(askpass);
48+
context.subscriptions.push(githubCredentialProviderManager);
4849

4950
const git = new Git({ gitPath: info.path, version: info.version, env });
5051
const model = new Model(git, askpass, context.globalState, outputChannel);

0 commit comments

Comments
 (0)