|
| 1 | +import { injectable } from 'inversify'; |
| 2 | +import findGit from 'find-git-exec'; |
| 3 | +import { dirname } from 'path'; |
| 4 | +import { pathExists } from 'fs-extra'; |
| 5 | +import { GitInit } from '@theia/git/lib/node/init/git-init'; |
| 6 | +import { DisposableCollection } from '@theia/core/lib/common/disposable'; |
| 7 | + |
| 8 | +@injectable() |
| 9 | +export class DefaultGitInit implements GitInit { |
| 10 | + |
| 11 | + protected readonly toDispose = new DisposableCollection(); |
| 12 | + |
| 13 | + async init(): Promise<void> { |
| 14 | + const { env } = process; |
| 15 | + try { |
| 16 | + const { execPath, path, version } = await findGit(); |
| 17 | + if (!!execPath && !!path && !!version) { |
| 18 | + const dir = dirname(dirname(path)); |
| 19 | + const [execPathOk, pathOk, dirOk] = await Promise.all([pathExists(execPath), pathExists(path), pathExists(dir)]); |
| 20 | + if (execPathOk && pathOk && dirOk) { |
| 21 | + if (typeof env.LOCAL_GIT_DIRECTORY !== 'undefined' && env.LOCAL_GIT_DIRECTORY !== dir) { |
| 22 | + console.error(`Misconfigured env.LOCAL_GIT_DIRECTORY: ${env.LOCAL_GIT_DIRECTORY}. dir was: ${dir}`); |
| 23 | + return; |
| 24 | + } |
| 25 | + if (typeof env.GIT_EXEC_PATH !== 'undefined' && env.GIT_EXEC_PATH !== execPath) { |
| 26 | + console.error(`Misconfigured env.GIT_EXEC_PATH: ${env.GIT_EXEC_PATH}. execPath was: ${execPath}`); |
| 27 | + return; |
| 28 | + } |
| 29 | + process.env.LOCAL_GIT_DIRECTORY = dir; |
| 30 | + process.env.GIT_EXEC_PATH = execPath; |
| 31 | + console.info(`Using Git [${version}] from the PATH. (${path})`); |
| 32 | + return; |
| 33 | + } |
| 34 | + } |
| 35 | + } catch (err) { |
| 36 | + console.error(err); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + dispose(): void { |
| 41 | + this.toDispose.dispose(); |
| 42 | + } |
| 43 | + |
| 44 | +} |
0 commit comments