Reworks remote parsing

Combines same url into same remote
Adds a change event for custom remote providers
Adds a repo change event for custom remote providers
This commit is contained in:
Eric Amodio
2017-09-12 15:46:44 -04:00
parent 48814d4213
commit ccc29e3dfc
16 changed files with 142 additions and 90 deletions

View File

@@ -14,6 +14,7 @@ export * from './parsers/blameParser';
export * from './parsers/branchParser';
export * from './parsers/diffParser';
export * from './parsers/logParser';
export * from './parsers/remoteParser';
export * from './parsers/stashParser';
export * from './parsers/statusParser';
export * from './remotes/provider';

View File

@@ -2,7 +2,7 @@
import { Disposable, Event, EventEmitter, TextDocument, TextDocumentChangeEvent, TextEditor, window, workspace } from 'vscode';
import { TextDocumentComparer } from '../comparers';
import { CommandContext, setCommandContext } from '../constants';
import { GitService, GitUri } from '../gitService';
import { GitService, GitUri, RepoChangedReasons } from '../gitService';
import { Logger } from '../logger';
export interface BlameabilityChangeEvent {
@@ -32,6 +32,7 @@ export class GitContextTracker extends Disposable {
subscriptions.push(workspace.onDidChangeConfiguration(this._onConfigurationChanged, this));
subscriptions.push(workspace.onDidSaveTextDocument(this._onTextDocumentSaved, this));
subscriptions.push(this.git.onDidBlameFail(this._onBlameFailed, this));
subscriptions.push(this.git.onDidChangeRepo(this._onRepoChanged, this));
this._disposable = Disposable.from(...subscriptions);
@@ -54,6 +55,13 @@ export class GitContextTracker extends Disposable {
}
}
async _onRepoChanged(reasons: RepoChangedReasons[]) {
if (!reasons.includes(RepoChangedReasons.Remotes)) return;
const gitUri = this._editor === undefined ? undefined : await GitUri.fromUri(this._editor.document.uri, this.git);
this._updateContextHasRemotes(gitUri);
}
private _onActiveTextEditorChanged(editor: TextEditor | undefined) {
this._editor = editor;
this._updateContext(this._gitEnabled ? editor : undefined);

View File

@@ -5,23 +5,9 @@ export type GitRemoteType = 'fetch' | 'push';
export class GitRemote {
name: string;
url: string;
type: GitRemoteType;
provider?: RemoteProvider;
constructor(remote: string) {
remote = remote.trim();
const [name, info] = remote.split('\t');
this.name = name;
const [url, typeInfo] = info.split(' ');
this.url = url;
this.type = typeInfo.substring(1, typeInfo.length - 1) as GitRemoteType;
this.provider = RemoteProviderFactory.getRemoteProvider(this.url);
constructor(public readonly repoPath: string, public readonly name: string, public readonly url: string, public readonly domain: string, public readonly path: string, public readonly types: GitRemoteType[]) {
this.provider = RemoteProviderFactory.getRemoteProvider(this.domain, this.path);
}
}

View File

@@ -0,0 +1,50 @@
'use strict';
import { GitRemote } from './../git';
import { GitRemoteType } from '../models/remote';
const remoteRegex = /^(.*)\t(.*)\s\((.*)\)$/gm;
const urlRegex = /^(?:git:\/\/(.*?)\/|https:\/\/(.*?)\/|http:\/\/(.*?)\/|git@(.*):|ssh:\/\/(?:.*@)?(.*?)(?::.*?)?\/)(.*)$/;
export class GitRemoteParser {
static parse(data: string, repoPath: string): GitRemote[] {
if (!data) return [];
const remotes: GitRemote[] = [];
const groups = Object.create(null);
let match: RegExpExecArray | null = null;
do {
match = remoteRegex.exec(data);
if (match == null) break;
const url = match[2];
const [domain, path] = this.parseGitUrl(url);
let remote: GitRemote | undefined = groups[url];
if (remote === undefined) {
remote = new GitRemote(repoPath, match[1], url, domain, path, [match[3] as GitRemoteType]);
remotes.push(remote);
groups[url] = remote;
}
else {
remote.types.push(match[3] as GitRemoteType);
}
} while (match != null);
if (!remotes.length) return [];
return remotes;
}
static parseGitUrl(url: string): [string, string] {
const match = urlRegex.exec(url);
if (match == null) return ['', ''];
return [
match[1] || match[2] || match[3] || match[4] || match[5],
match[6].replace(/\.git\/?$/, '')
];
}
}

View File

@@ -1,5 +1,6 @@
'use strict';
import { ExtensionContext, workspace } from 'vscode';
import { Objects } from '../../system';
import { Event, EventEmitter, ExtensionContext, workspace } from 'vscode';
import { BitbucketService } from './bitbucket';
import { BitbucketServerService } from './bitbucket-server';
import { CustomRemoteType, IConfig, IRemotesConfig } from '../../configuration';
@@ -9,22 +10,9 @@ import { GitLabService } from './gitlab';
import { Logger } from '../../logger';
import { RemoteProvider } from './provider';
import { VisualStudioService } from './visualStudio';
import { Objects } from '../../system';
export { RemoteProvider };
const UrlRegex = /^(?:git:\/\/(.*?)\/|https:\/\/(.*?)\/|http:\/\/(.*?)\/|git@(.*):|ssh:\/\/(?:.*@)?(.*?)(?::.*?)?\/)(.*)$/;
function getCustomProvider(type: CustomRemoteType) {
switch (type) {
case CustomRemoteType.Bitbucket: return (domain: string, path: string) => new BitbucketService(domain, path, true);
case CustomRemoteType.BitbucketServer: return (domain: string, path: string) => new BitbucketServerService(domain, path, true);
case CustomRemoteType.GitHub: return (domain: string, path: string) => new GitHubService(domain, path, true);
case CustomRemoteType.GitLab: return (domain: string, path: string) => new GitLabService(domain, path, true);
}
return undefined;
}
const defaultProviderMap = new Map<string, (domain: string, path: string) => RemoteProvider>([
['bitbucket.org', (domain: string, path: string) => new BitbucketService(domain, path)],
['github.com', (domain: string, path: string) => new GitHubService(domain, path)],
@@ -32,48 +20,29 @@ const defaultProviderMap = new Map<string, (domain: string, path: string) => Rem
['visualstudio.com', (domain: string, path: string) => new VisualStudioService(domain, path)]
]);
let providerMap: Map<string, (domain: string, path: string) => RemoteProvider>;
let remotesCfg: IRemotesConfig[];
function onConfigurationChanged() {
const cfg = workspace.getConfiguration().get<IConfig>(ExtensionKey);
if (cfg === undefined) return;
if (!Objects.areEquivalent(cfg.remotes, remotesCfg)) {
providerMap = new Map(defaultProviderMap);
remotesCfg = cfg.remotes;
if (remotesCfg != null && remotesCfg.length > 0) {
for (const svc of remotesCfg) {
const provider = getCustomProvider(svc.type);
if (provider === undefined) continue;
providerMap.set(svc.domain.toLowerCase(), provider);
}
}
}
}
export class RemoteProviderFactory {
static configure(context: ExtensionContext) {
context.subscriptions.push(workspace.onDidChangeConfiguration(onConfigurationChanged));
onConfigurationChanged();
private static _providerMap: Map<string, (domain: string, path: string) => RemoteProvider>;
private static _remotesCfg: IRemotesConfig[];
private static _onDidChange = new EventEmitter<void>();
public static get onDidChange(): Event<void> {
return this._onDidChange.event;
}
static getRemoteProvider(url: string): RemoteProvider | undefined {
static configure(context: ExtensionContext) {
context.subscriptions.push(workspace.onDidChangeConfiguration(() => this.onConfigurationChanged()));
this.onConfigurationChanged(true);
}
static getRemoteProvider(domain: string, path: string): RemoteProvider | undefined {
try {
const match = UrlRegex.exec(url);
if (match == null) return undefined;
let key = domain.toLowerCase();
if (key.endsWith('visualstudio.com')) {
key = 'visualstudio.com';
}
const domain = match[1] || match[2] || match[3] || match[4] || match[5];
const path = match[6].replace(/\.git\/?$/, '');
const key = domain.toLowerCase().endsWith('visualstudio.com')
? 'visualstudio.com'
: domain;
const creator = providerMap.get(key.toLowerCase());
const creator = this._providerMap.get(key);
if (creator === undefined) return undefined;
return creator(domain, path);
@@ -83,4 +52,37 @@ export class RemoteProviderFactory {
return undefined;
}
}
private static onConfigurationChanged(silent: boolean = false) {
const cfg = workspace.getConfiguration().get<IConfig>(ExtensionKey);
if (cfg === undefined) return;
if (!Objects.areEquivalent(cfg.remotes, this._remotesCfg)) {
this._providerMap = new Map(defaultProviderMap);
this._remotesCfg = cfg.remotes;
if (this._remotesCfg != null && this._remotesCfg.length > 0) {
for (const svc of this._remotesCfg) {
const provider = this.getCustomProvider(svc.type);
if (provider === undefined) continue;
this._providerMap.set(svc.domain.toLowerCase(), provider);
}
if (!silent) {
this._onDidChange.fire();
}
}
}
}
private static getCustomProvider(type: CustomRemoteType) {
switch (type) {
case CustomRemoteType.Bitbucket: return (domain: string, path: string) => new BitbucketService(domain, path, true);
case CustomRemoteType.BitbucketServer: return (domain: string, path: string) => new BitbucketServerService(domain, path, true);
case CustomRemoteType.GitHub: return (domain: string, path: string) => new GitHubService(domain, path, true);
case CustomRemoteType.GitLab: return (domain: string, path: string) => new GitLabService(domain, path, true);
}
return undefined;
}
}