You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

302 lines
12 KiB

  1. 'use strict';
  2. import { commands, ExtensionContext, extensions, window, workspace } from 'vscode';
  3. import { Commands, registerCommands } from './commands';
  4. import { Config, configuration, Configuration } from './configuration';
  5. import { CommandContext, extensionQualifiedId, GlobalState, GlyphChars, setCommandContext } from './constants';
  6. import { Container } from './container';
  7. import { GitCommit, GitService, GitUri } from './git/gitService';
  8. import { Logger } from './logger';
  9. import { Messages } from './messages';
  10. import { Strings, Versions } from './system';
  11. import { ModeConfig } from './ui/config';
  12. // import { Telemetry } from './telemetry';
  13. export async function activate(context: ExtensionContext) {
  14. const start = process.hrtime();
  15. // Pretend we are enabled (until we know otherwise) and set the view contexts to reduce flashing on load
  16. setCommandContext(CommandContext.Enabled, true);
  17. Logger.configure(context, o => {
  18. if (o instanceof GitUri) {
  19. return `GitUri(${o.toString(true)}${o.repoPath ? ` repoPath=${o.repoPath}` : ''}${
  20. o.sha ? ` sha=${o.sha}` : ''
  21. })`;
  22. }
  23. if (o instanceof GitCommit) {
  24. return `GitCommit(${o.sha ? ` sha=${o.sha}` : ''}${o.repoPath ? ` repoPath=${o.repoPath}` : ''})`;
  25. }
  26. return undefined;
  27. });
  28. const gitlens = extensions.getExtension(extensionQualifiedId)!;
  29. const gitlensVersion = gitlens.packageJSON.version;
  30. const enabled = workspace.getConfiguration('git', null!).get<boolean>('enabled', true);
  31. if (!enabled) {
  32. Logger.log(`GitLens(v${gitlensVersion}) was NOT activated -- "git.enabled": false`);
  33. setCommandContext(CommandContext.Enabled, false);
  34. void Messages.showGitDisabledErrorMessage();
  35. return;
  36. }
  37. Configuration.configure(context);
  38. const cfg = configuration.get<Config>();
  39. const previousVersion = context.globalState.get<string>(GlobalState.GitLensVersion);
  40. await migrateSettings(context, previousVersion);
  41. try {
  42. await GitService.initialize();
  43. }
  44. catch (ex) {
  45. Logger.error(ex, `GitLens(v${gitlensVersion}).activate`);
  46. setCommandContext(CommandContext.Enabled, false);
  47. if (ex.message.includes('Unable to find git')) {
  48. await window.showErrorMessage(
  49. `GitLens was unable to find Git. Please make sure Git is installed. Also ensure that Git is either in the PATH, or that 'git.path' is pointed to its installed location.`
  50. );
  51. }
  52. return;
  53. }
  54. Container.initialize(context, cfg);
  55. registerCommands(context);
  56. const gitVersion = GitService.getGitVersion();
  57. // Telemetry.configure(ApplicationInsightsKey);
  58. // const telemetryContext: { [id: string]: any } = Object.create(null);
  59. // telemetryContext.version = gitlensVersion;
  60. // telemetryContext['git.version'] = gitVersion;
  61. // Telemetry.setContext(telemetryContext);
  62. notifyOnUnsupportedGitVersion(gitVersion);
  63. void showWelcomePage(gitlensVersion, previousVersion);
  64. context.globalState.update(GlobalState.GitLensVersion, gitlensVersion);
  65. // Constantly over my data cap so stop collecting initialized event
  66. // Telemetry.trackEvent('initialized', Objects.flatten(cfg, 'config', true));
  67. Logger.log(`GitLens(v${gitlensVersion}) activated ${GlyphChars.Dot} ${Strings.getDurationMilliseconds(start)} ms`);
  68. }
  69. export function deactivate() {}
  70. async function migrateSettings(context: ExtensionContext, previousVersion: string | undefined) {
  71. if (previousVersion === undefined) return;
  72. const previous = Versions.fromString(previousVersion);
  73. try {
  74. if (Versions.compare(previous, Versions.from(9, 0, 0)) !== 1) {
  75. await configuration.migrate(
  76. 'gitExplorer.autoRefresh',
  77. configuration.name('views')('repositories')('autoRefresh').value
  78. );
  79. await configuration.migrate(
  80. 'gitExplorer.branches.layout',
  81. configuration.name('views')('repositories')('branches')('layout').value
  82. );
  83. await configuration.migrate(
  84. 'gitExplorer.enabled',
  85. configuration.name('views')('repositories')('enabled').value
  86. );
  87. await configuration.migrate(
  88. 'gitExplorer.files.compact',
  89. configuration.name('views')('repositories')('files')('compact').value
  90. );
  91. await configuration.migrate(
  92. 'gitExplorer.files.layout',
  93. configuration.name('views')('repositories')('files')('layout').value
  94. );
  95. await configuration.migrate(
  96. 'gitExplorer.files.threshold',
  97. configuration.name('views')('repositories')('files')('threshold').value
  98. );
  99. await configuration.migrate(
  100. 'gitExplorer.includeWorkingTree',
  101. configuration.name('views')('repositories')('includeWorkingTree').value
  102. );
  103. await configuration.migrate(
  104. 'gitExplorer.location',
  105. configuration.name('views')('repositories')('location').value
  106. );
  107. await configuration.migrate(
  108. 'gitExplorer.showTrackingBranch',
  109. configuration.name('views')('repositories')('showTrackingBranch').value
  110. );
  111. await configuration.migrate(
  112. 'historyExplorer.avatars',
  113. configuration.name('views')('fileHistory')('avatars').value
  114. );
  115. await configuration.migrate(
  116. 'historyExplorer.enabled',
  117. configuration.name('views')('fileHistory')('enabled').value
  118. );
  119. await configuration.migrate(
  120. 'historyExplorer.location',
  121. configuration.name('views')('fileHistory')('location').value
  122. );
  123. await configuration.migrate(
  124. 'historyExplorer.avatars',
  125. configuration.name('views')('lineHistory')('avatars').value
  126. );
  127. await configuration.migrate(
  128. 'historyExplorer.enabled',
  129. configuration.name('views')('lineHistory')('enabled').value
  130. );
  131. await configuration.migrate(
  132. 'historyExplorer.location',
  133. configuration.name('views')('lineHistory')('location').value
  134. );
  135. await configuration.migrate(
  136. 'resultsExplorer.files.compact',
  137. configuration.name('views')('compare')('files')('compact').value
  138. );
  139. await configuration.migrate(
  140. 'resultsExplorer.files.layout',
  141. configuration.name('views')('compare')('files')('layout').value
  142. );
  143. await configuration.migrate(
  144. 'resultsExplorer.files.threshold',
  145. configuration.name('views')('compare')('files')('threshold').value
  146. );
  147. await configuration.migrate(
  148. 'resultsExplorer.location',
  149. configuration.name('views')('compare')('location').value
  150. );
  151. await configuration.migrate(
  152. 'resultsExplorer.files.compact',
  153. configuration.name('views')('search')('files')('compact').value
  154. );
  155. await configuration.migrate(
  156. 'resultsExplorer.files.layout',
  157. configuration.name('views')('search')('files')('layout').value
  158. );
  159. await configuration.migrate(
  160. 'resultsExplorer.files.threshold',
  161. configuration.name('views')('search')('files')('threshold').value
  162. );
  163. await configuration.migrate(
  164. 'resultsExplorer.location',
  165. configuration.name('views')('search')('location').value
  166. );
  167. await configuration.migrate('explorers.avatars', configuration.name('views')('avatars').value);
  168. await configuration.migrate(
  169. 'explorers.commitFileFormat',
  170. configuration.name('views')('commitFileFormat').value
  171. );
  172. await configuration.migrate('explorers.commitFormat', configuration.name('views')('commitFormat').value);
  173. await configuration.migrate(
  174. 'explorers.defaultItemLimit',
  175. configuration.name('views')('defaultItemLimit').value
  176. );
  177. await configuration.migrate(
  178. 'explorers.files.compact',
  179. configuration.name('views')('files')('compact').value
  180. );
  181. await configuration.migrate('explorers.files.layout', configuration.name('views')('files')('layout').value);
  182. await configuration.migrate(
  183. 'explorers.files.threshold',
  184. configuration.name('views')('files')('threshold').value
  185. );
  186. await configuration.migrate(
  187. 'explorers.stashFileFormat',
  188. configuration.name('views')('stashFileFormat').value
  189. );
  190. await configuration.migrate('explorers.stashFormat', configuration.name('views')('stashFormat').value);
  191. await configuration.migrate(
  192. 'explorers.statusFileFormat',
  193. configuration.name('views')('statusFileFormat').value
  194. );
  195. await configuration.migrate<
  196. {
  197. [key: string]: {
  198. name: string;
  199. statusBarItemName?: string;
  200. description?: string;
  201. codeLens?: boolean;
  202. currentLine?: boolean;
  203. explorers?: boolean;
  204. hovers?: boolean;
  205. statusBar?: boolean;
  206. };
  207. },
  208. {
  209. [key: string]: ModeConfig;
  210. }
  211. >('modes', configuration.name('modes').value, {
  212. migrationFn: v => {
  213. const modes = Object.create(null);
  214. for (const k in v) {
  215. const { explorers, ...mode } = v[k];
  216. modes[k] = { ...mode, views: explorers };
  217. }
  218. return modes;
  219. }
  220. });
  221. }
  222. }
  223. catch (ex) {
  224. Logger.error(ex, 'migrateSettings');
  225. }
  226. }
  227. function notifyOnUnsupportedGitVersion(version: string) {
  228. if (GitService.compareGitVersion('2.2.0') !== -1) return;
  229. // If git is less than v2.2.0
  230. void Messages.showGitVersionUnsupportedErrorMessage(version);
  231. }
  232. async function showWelcomePage(version: string, previousVersion: string | undefined) {
  233. if (previousVersion === undefined) {
  234. Logger.log(`GitLens first-time install`);
  235. if (Container.config.showWhatsNewAfterUpgrades) {
  236. await commands.executeCommand(Commands.ShowWelcomePage);
  237. }
  238. return;
  239. }
  240. if (previousVersion !== version) {
  241. Logger.log(`GitLens upgraded from v${previousVersion} to v${version}`);
  242. }
  243. const [major, minor] = version.split('.');
  244. const [prevMajor, prevMinor] = previousVersion.split('.');
  245. if (
  246. (major === prevMajor && minor === prevMinor) ||
  247. // Don't notify on downgrades
  248. (major < prevMajor || (major === prevMajor && minor < prevMinor))
  249. ) {
  250. return;
  251. }
  252. if (Container.config.showWhatsNewAfterUpgrades) {
  253. await commands.executeCommand(Commands.ShowWelcomePage);
  254. }
  255. else {
  256. await Messages.showWhatsNewMessage(version);
  257. }
  258. }