-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
extension.ts
310 lines (252 loc) · 10.3 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
'use strict';
import { commands, ExtensionContext, extensions, window, workspace } from 'vscode';
import { CreatePullRequestActionContext, GitLensApi, OpenPullRequestActionContext } from '../src/api/gitlens';
import { Api } from './api/api';
import { Commands, executeCommand, OpenPullRequestOnRemoteCommandArgs, registerCommands } from './commands';
import { CreatePullRequestOnRemoteCommandArgs } from './commands/createPullRequestOnRemote';
import { configuration, Configuration, TraceLevel } from './configuration';
import { ContextKeys, GlobalState, GlyphChars, setContext, SyncedState } from './constants';
import { Container } from './container';
import { Git, GitBranch, GitCommit } from './git/git';
import { GitService } from './git/gitService';
import { GitUri } from './git/gitUri';
import { InvalidGitConfigError, UnableToFindGitError } from './git/locator';
import { Logger } from './logger';
import { Messages } from './messages';
import { registerPartnerActionRunners } from './partners';
import { Strings, Versions } from './system';
import { ViewNode } from './views/nodes';
let _context: ExtensionContext | undefined;
export async function activate(context: ExtensionContext): Promise<GitLensApi | undefined> {
const start = process.hrtime();
_context = context;
if (context.extension.id === 'eamodio.gitlens-insiders') {
// Ensure that stable isn't also installed
const stable = extensions.getExtension('eamodio.gitlens');
if (stable != null) {
Logger.log('GitLens (Insiders) was NOT activated because GitLens is also enabled');
// If we don't use a setTimeout here this notification will get lost for some reason
setTimeout(() => void Messages.showInsidersErrorMessage(), 0);
return undefined;
}
}
// Pretend we are enabled (until we know otherwise) and set the view contexts to reduce flashing on load
void setContext(ContextKeys.Enabled, true);
if (!workspace.isTrusted) {
void setContext(ContextKeys.Readonly, true);
context.subscriptions.push(
workspace.onDidGrantWorkspaceTrust(() => void setContext(ContextKeys.Readonly, undefined)),
);
}
setKeysForSync();
Logger.configure(context, configuration.get('outputLevel'), o => {
if (GitUri.is(o)) {
return `GitUri(${o.toString(true)}${o.repoPath ? ` repoPath=${o.repoPath}` : ''}${
o.sha ? ` sha=${o.sha}` : ''
})`;
}
if (GitCommit.is(o)) {
return `GitCommit(${o.sha ? ` sha=${o.sha}` : ''}${o.repoPath ? ` repoPath=${o.repoPath}` : ''})`;
}
if (ViewNode.is(o)) {
return o.toString();
}
return undefined;
});
const gitlensVersion = context.extension.packageJSON.version;
const syncedVersion = context.globalState.get<string>(SyncedState.Version);
const localVersion =
context.globalState.get<string>(GlobalState.Version) ??
context.globalState.get<string>(GlobalState.Deprecated_Version);
let previousVersion;
if (localVersion == null || syncedVersion == null) {
previousVersion = syncedVersion ?? localVersion;
} else if (Versions.compare(syncedVersion, localVersion) === 1) {
previousVersion = syncedVersion;
} else {
previousVersion = localVersion;
}
if (Logger.willLog('debug')) {
Logger.debug(
`GitLens (v${gitlensVersion}): syncedVersion=${syncedVersion}, localVersion=${localVersion}, previousVersion=${previousVersion}, ${
SyncedState.WelcomeViewVisible
}=${context.globalState.get<boolean>(SyncedState.WelcomeViewVisible)}`,
);
}
if (previousVersion == null) {
void context.globalState.update(SyncedState.WelcomeViewVisible, true);
void setContext(ContextKeys.ViewsWelcomeVisible, true);
} else {
void setContext(
ContextKeys.ViewsWelcomeVisible,
context.globalState.get<boolean>(SyncedState.WelcomeViewVisible) ?? false,
);
}
const enabled = workspace.getConfiguration('git', null).get<boolean>('enabled', true);
if (!enabled) {
Logger.log(`GitLens (v${gitlensVersion}) was NOT activated -- "git.enabled": false`);
void setEnabled(false);
void Messages.showGitDisabledErrorMessage();
return undefined;
}
Configuration.configure(context);
const cfg = configuration.get();
// await migrateSettings(context, previousVersion);
try {
await GitService.initialize();
} catch (ex) {
Logger.error(ex, `GitLens (v${gitlensVersion}) activate`);
void setEnabled(false);
if (ex instanceof InvalidGitConfigError) {
void Messages.showGitInvalidConfigErrorMessage();
} else if (ex instanceof UnableToFindGitError) {
void Messages.showGitMissingErrorMessage();
} else {
const msg: string = ex?.message ?? '';
if (msg) {
void window.showErrorMessage(`Unable to initialize Git; ${msg}`);
}
}
return undefined;
}
Container.initialize(context, cfg);
registerCommands(context);
registerBuiltInActionRunners(context);
registerPartnerActionRunners(context);
const gitVersion = Git.getGitVersion();
notifyOnUnsupportedGitVersion(gitVersion);
void showWelcomeOrWhatsNew(context, gitlensVersion, previousVersion);
void context.globalState.update(GlobalState.Version, gitlensVersion);
// Only update our synced version if the new version is greater
if (syncedVersion == null || Versions.compare(gitlensVersion, syncedVersion) === 1) {
void context.globalState.update(SyncedState.Version, gitlensVersion);
}
if (cfg.outputLevel === TraceLevel.Debug) {
setTimeout(async () => {
if (cfg.outputLevel !== TraceLevel.Debug) return;
if (await Messages.showDebugLoggingWarningMessage()) {
void commands.executeCommand(Commands.DisableDebugLogging);
}
}, 60000);
}
Logger.log(
`GitLens (v${gitlensVersion}${cfg.mode.active ? `, mode: ${cfg.mode.active}` : ''}) activated ${
GlyphChars.Dot
} ${Strings.getDurationMilliseconds(start)} ms`,
);
const api = new Api();
return api;
}
export function deactivate() {
// nothing to do
}
// async function migrateSettings(context: ExtensionContext, previousVersion: string | undefined) {
// if (previousVersion === undefined) return;
// const previous = Versions.fromString(previousVersion);
// try {
// if (Versions.compare(previous, Versions.from(11, 0, 0)) !== 1) {
// }
// } catch (ex) {
// Logger.error(ex, 'migrateSettings');
// }
// }
export async function setEnabled(enabled: boolean): Promise<void> {
await Promise.all([setContext(ContextKeys.Enabled, enabled), setContext(ContextKeys.Disabled, !enabled)]);
}
export function setKeysForSync(...keys: (SyncedState | string)[]) {
return _context?.globalState.setKeysForSync([...keys, SyncedState.Version, SyncedState.WelcomeViewVisible]);
}
export function notifyOnUnsupportedGitVersion(version: string) {
if (GitService.compareGitVersion('2.7.2') !== -1) return;
// If git is less than v2.7.2
void Messages.showGitVersionUnsupportedErrorMessage(version, '2.7.2');
}
function registerBuiltInActionRunners(context: ExtensionContext): void {
context.subscriptions.push(
Container.actionRunners.registerBuiltIn<CreatePullRequestActionContext>('createPullRequest', {
label: ctx => `Create Pull Request on ${ctx.remote?.provider?.name ?? 'Remote'}`,
run: async ctx => {
if (ctx.type !== 'createPullRequest') return;
void (await executeCommand<CreatePullRequestOnRemoteCommandArgs>(Commands.CreatePullRequestOnRemote, {
base: undefined,
compare: ctx.branch.isRemote
? GitBranch.getNameWithoutRemote(ctx.branch.name)
: ctx.branch.upstream
? GitBranch.getNameWithoutRemote(ctx.branch.upstream)
: ctx.branch.name,
remote: ctx.remote?.name ?? '',
repoPath: ctx.repoPath,
}));
},
}),
Container.actionRunners.registerBuiltIn<OpenPullRequestActionContext>('openPullRequest', {
label: ctx => `Open Pull Request on ${ctx.provider?.name ?? 'Remote'}`,
run: async ctx => {
if (ctx.type !== 'openPullRequest') return;
void (await executeCommand<OpenPullRequestOnRemoteCommandArgs>(Commands.OpenPullRequestOnRemote, {
pr: { url: ctx.pullRequest.url },
}));
},
}),
);
}
async function showWelcomeOrWhatsNew(context: ExtensionContext, version: string, previousVersion: string | undefined) {
if (previousVersion == null) {
Logger.log(`GitLens first-time install; window.focused=${window.state.focused}`);
if (Container.config.showWelcomeOnInstall === false) return;
if (window.state.focused) {
await context.globalState.update(GlobalState.PendingWelcomeOnFocus, undefined);
await commands.executeCommand(Commands.ShowWelcomePage);
} else {
// Save pending on window getting focus
await context.globalState.update(GlobalState.PendingWelcomeOnFocus, true);
const disposable = window.onDidChangeWindowState(e => {
if (!e.focused) return;
disposable.dispose();
// If the window is now focused and we are pending the welcome, clear the pending state and show the welcome
if (context.globalState.get(GlobalState.PendingWelcomeOnFocus) === true) {
void context.globalState.update(GlobalState.PendingWelcomeOnFocus, undefined);
if (Container.config.showWelcomeOnInstall) {
void commands.executeCommand(Commands.ShowWelcomePage);
}
}
});
context.subscriptions.push(disposable);
}
return;
}
if (previousVersion !== version) {
Logger.log(`GitLens upgraded from v${previousVersion} to v${version}; window.focused=${window.state.focused}`);
}
const [major, minor] = version.split('.').map(v => parseInt(v, 10));
const [prevMajor, prevMinor] = previousVersion.split('.').map(v => parseInt(v, 10));
if (
(major === prevMajor && minor === prevMinor) ||
// Don't notify on downgrades
major < prevMajor ||
(major === prevMajor && minor < prevMinor)
) {
return;
}
if (major !== prevMajor && Container.config.showWhatsNewAfterUpgrades) {
if (window.state.focused) {
await context.globalState.update(GlobalState.PendingWhatsNewOnFocus, undefined);
await Messages.showWhatsNewMessage(version);
} else {
// Save pending on window getting focus
await context.globalState.update(GlobalState.PendingWhatsNewOnFocus, true);
const disposable = window.onDidChangeWindowState(e => {
if (!e.focused) return;
disposable.dispose();
// If the window is now focused and we are pending the what's new, clear the pending state and show the what's new
if (context.globalState.get(GlobalState.PendingWhatsNewOnFocus) === true) {
void context.globalState.update(GlobalState.PendingWhatsNewOnFocus, undefined);
if (Container.config.showWhatsNewAfterUpgrades) {
void Messages.showWhatsNewMessage(version);
}
}
});
context.subscriptions.push(disposable);
}
}
}