fix: git extension

This commit is contained in:
iCrawl 2019-11-03 04:17:58 +01:00
parent ef71c60fc5
commit 8af1c12dc4
No known key found for this signature in database
GPG key ID: E41A6DB922EC2CFE
4 changed files with 46 additions and 21 deletions

View file

@ -13,7 +13,7 @@ export default class RPCClient implements Disposable {
public config = workspace.getConfiguration('discord'); public config = workspace.getConfiguration('discord');
public git!: API; public git?: API;
private _rpc: any; private _rpc: any;

View file

@ -4,7 +4,6 @@ import Logger from './structures/Logger';
import { GitExtension } from './git'; import { GitExtension } from './git';
const { register } = require('discord-rpc'); // eslint-disable-line const { register } = require('discord-rpc'); // eslint-disable-line
const sleep = (wait: number) => new Promise(resolve => setTimeout(resolve, wait));
let loginTimeout: NodeJS.Timer; let loginTimeout: NodeJS.Timer;
const statusBarIcon: StatusBarItem = window.createStatusBarItem(StatusBarAlignment.Left); const statusBarIcon: StatusBarItem = window.createStatusBarItem(StatusBarAlignment.Left);
@ -15,18 +14,6 @@ register(config.get<string>('clientID')!);
const rpc = new RPCClient(config.get<string>('clientID')!, statusBarIcon); const rpc = new RPCClient(config.get<string>('clientID')!, statusBarIcon);
export async function activate(context: ExtensionContext) { export async function activate(context: ExtensionContext) {
try {
const ext = extensions.getExtension<GitExtension>('vscode.git')!;
await ext.activate();
rpc.git = ext.exports.getAPI(1);
} catch {
// We loaded before the git extension, give it a bit to load
// In a perfect world this shouldn't happen
await sleep(2000);
const ext = extensions.getExtension<GitExtension>('vscode.git')!;
await ext.activate();
rpc.git = ext.exports.getAPI(1);
}
Logger.log('Discord Presence activated!'); Logger.log('Discord Presence activated!');
let isWorkspaceExcluded = false; let isWorkspaceExcluded = false;
@ -105,6 +92,17 @@ export async function activate(context: ExtensionContext) {
disableJoinRequests, disableJoinRequests,
); );
const gitExtension = extensions.getExtension<GitExtension>('vscode.git');
if (gitExtension) {
if (!gitExtension.exports.enabled) {
gitExtension.exports.onDidChangeEnablement(e => {
if (e) {
rpc.git = gitExtension.exports.getAPI(1);
}
});
}
}
if (!isWorkspaceExcluded && config.get<boolean>('enabled')) { if (!isWorkspaceExcluded && config.get<boolean>('enabled')) {
statusBarIcon.show(); statusBarIcon.show();
try { try {

33
src/git.d.ts vendored
View file

@ -3,7 +3,8 @@
* Licensed under the MIT License. See License.txt in the project root for license information. * Licensed under the MIT License. See License.txt in the project root for license information.
*/ */
import { Uri, Event } from 'vscode'; // eslint-disable-next-line
import { Uri, SourceControlInputBox, Event, CancellationToken } from 'vscode';
export interface Git { export interface Git {
readonly path: string; readonly path: string;
@ -41,6 +42,7 @@ export interface Commit {
readonly hash: string; readonly hash: string;
readonly message: string; readonly message: string;
readonly parents: string[]; readonly parents: string[];
readonly authorEmail?: string | undefined;
} }
export interface Submodule { export interface Submodule {
@ -67,6 +69,7 @@ export const enum Status {
DELETED, DELETED,
UNTRACKED, UNTRACKED,
IGNORED, IGNORED,
INTENT_TO_ADD,
ADDED_BY_US, ADDED_BY_US,
ADDED_BY_THEM, ADDED_BY_THEM,
@ -108,6 +111,14 @@ export interface RepositoryUIState {
readonly onDidChange: Event<void>; readonly onDidChange: Event<void>;
} }
/**
* Log options.
*/
export interface LogOptions {
/** Max number of log entries to retrieve. If not specified, the default is 32. */
readonly maxEntries?: number;
}
export interface Repository { export interface Repository {
readonly rootUri: Uri; readonly rootUri: Uri;
readonly inputBox: InputBox; readonly inputBox: InputBox;
@ -117,6 +128,7 @@ export interface Repository {
getConfigs(): Promise<{ key: string; value: string }[]>; getConfigs(): Promise<{ key: string; value: string }[]>;
getConfig(key: string): Promise<string>; getConfig(key: string): Promise<string>;
setConfig(key: string, value: string): Promise<string>; setConfig(key: string, value: string): Promise<string>;
getGlobalConfig(key: string): Promise<string>;
getObjectDetails(treeish: string, path: string): Promise<{ mode: string; object: string; size: number }>; getObjectDetails(treeish: string, path: string): Promise<{ mode: string; object: string; size: number }>;
detectObjectType(object: string): Promise<{ mimetype: string; encoding?: string }>; detectObjectType(object: string): Promise<{ mimetype: string; encoding?: string }>;
@ -128,11 +140,16 @@ export interface Repository {
apply(patch: string, reverse?: boolean): Promise<void>; apply(patch: string, reverse?: boolean): Promise<void>;
diff(cached?: boolean): Promise<string>; diff(cached?: boolean): Promise<string>;
diffWithHEAD(): Promise<Change[]>;
diffWithHEAD(path: string): Promise<string>; diffWithHEAD(path: string): Promise<string>;
diffWith(ref: string): Promise<Change[]>;
diffWith(ref: string, path: string): Promise<string>; diffWith(ref: string, path: string): Promise<string>;
diffIndexWithHEAD(): Promise<Change[]>;
diffIndexWithHEAD(path: string): Promise<string>; diffIndexWithHEAD(path: string): Promise<string>;
diffIndexWith(ref: string): Promise<Change[]>;
diffIndexWith(ref: string, path: string): Promise<string>; diffIndexWith(ref: string, path: string): Promise<string>;
diffBlobs(object1: string, object2: string): Promise<string>; diffBlobs(object1: string, object2: string): Promise<string>;
diffBetween(ref1: string, ref2: string): Promise<Change[]>;
diffBetween(ref1: string, ref2: string, path: string): Promise<string>; diffBetween(ref1: string, ref2: string, path: string): Promise<string>;
hashObject(data: string): Promise<string>; hashObject(data: string): Promise<string>;
@ -150,12 +167,19 @@ export interface Repository {
addRemote(name: string, url: string): Promise<void>; addRemote(name: string, url: string): Promise<void>;
removeRemote(name: string): Promise<void>; removeRemote(name: string): Promise<void>;
fetch(remote?: string, ref?: string): Promise<void>; fetch(remote?: string, ref?: string, depth?: number): Promise<void>;
pull(): Promise<void>; pull(unshallow?: boolean): Promise<void>;
push(remoteName?: string, branchName?: string, setUpstream?: boolean): Promise<void>; push(remoteName?: string, branchName?: string, setUpstream?: boolean): Promise<void>;
blame(path: string): Promise<string>;
log(options?: LogOptions): Promise<Commit[]>;
} }
export type APIState = 'uninitialized' | 'initialized';
export interface API { export interface API {
readonly state: APIState;
readonly onDidChangeState: Event<APIState>;
readonly git: Git; readonly git: Git;
readonly repositories: Repository[]; readonly repositories: Repository[];
readonly onDidOpenRepository: Event<Repository>; readonly onDidOpenRepository: Event<Repository>;
@ -211,4 +235,7 @@ export const enum GitErrorCodes {
WrongCase = 'WrongCase', WrongCase = 'WrongCase',
CantLockRef = 'CantLockRef', CantLockRef = 'CantLockRef',
CantRebaseMultipleBranches = 'CantRebaseMultipleBranches', CantRebaseMultipleBranches = 'CantRebaseMultipleBranches',
PatchDoesNotApply = 'PatchDoesNotApply',
NoPathFound = 'NoPathFound',
UnknownPath = 'UnknownPath',
} }

View file

@ -337,7 +337,7 @@ export default class Activity implements Disposable {
} }
if (str.includes('{gitbranch}')) { if (str.includes('{gitbranch}')) {
if (this.client.git.repositories.length) { if (this.client.git?.repositories.length) {
fileDetail.gitbranch = this.client.git.repositories.find(repo => repo.ui.selected)!.state.HEAD!.name; fileDetail.gitbranch = this.client.git.repositories.find(repo => repo.ui.selected)!.state.HEAD!.name;
} else { } else {
fileDetail.gitbranch = 'Unknown'; fileDetail.gitbranch = 'Unknown';
@ -345,7 +345,7 @@ export default class Activity implements Disposable {
} }
if (str.includes('{gitreponame}')) { if (str.includes('{gitreponame}')) {
if (this.client.git.repositories.length) { if (this.client.git?.repositories.length) {
fileDetail.gitreponame = this.client.git.repositories fileDetail.gitreponame = this.client.git.repositories
.find(repo => repo.ui.selected)! .find(repo => repo.ui.selected)!
.state.remotes[0].fetchUrl!.split('/')[1] .state.remotes[0].fetchUrl!.split('/')[1]