Skip to content

Commit

Permalink
Improves logging of repo search at start
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Apr 7, 2019
1 parent 1d924e2 commit fb36df4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 44 deletions.
62 changes: 29 additions & 33 deletions src/git/gitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
// eslint-disable-next-line import/no-unresolved
import { API as BuiltInGitApi, GitExtension } from '../@types/git';
import { configuration, RemotesConfig } from '../configuration';
import { CommandContext, DocumentSchemes, GlyphChars, setCommandContext } from '../constants';
import { CommandContext, DocumentSchemes, setCommandContext } from '../constants';
import { Container } from '../container';
import { LogCorrelationContext, Logger } from '../logger';
import { Messages } from '../messages';
Expand Down Expand Up @@ -265,32 +265,32 @@ export class GitService implements Disposable {
}
}

@log<GitService['repositorySearch']>({
args: false,
singleLine: true,
prefix: (context, folder) => `${context.prefix}(${folder.uri.fsPath})`,
exit: result =>
`returned ${result.length} repositories${
result.length !== 0 ? ` (${result.map(r => r.path).join(', ')})` : emptyStr
}`
})
private async repositorySearch(folder: WorkspaceFolder): Promise<Repository[]> {
const cc = Logger.getCorrelationContext();
const { uri } = folder;
const depth = configuration.get<number>(configuration.name('advanced')('repositorySearchDepth').value, uri);

Logger.log(`Searching for repositories (depth=${depth}) in '${uri.fsPath}' ...`);

const start = process.hrtime();
Logger.log(cc, `searching (depth=${depth})...`);

const repositories: Repository[] = [];
const anyRepoChangedFn = this.onAnyRepositoryChanged.bind(this);

const rootPath = await this.getRepoPathCore(uri.fsPath, true);
if (rootPath !== undefined) {
Logger.log(`Repository found in '${rootPath}'`);
Logger.log(cc, `found root repository in '${rootPath}'`);
repositories.push(new Repository(folder, rootPath, true, anyRepoChangedFn, this._suspended));
}

if (depth <= 0) {
Logger.log(
`Completed repository search (depth=${depth}) in '${uri.fsPath}' ${
GlyphChars.Dot
} ${Strings.getDurationMilliseconds(start)} ms`
);

return repositories;
}
if (depth <= 0) return repositories;

// Get any specified excludes -- this is a total hack, but works for some simple cases and something is better than nothing :)
let excludes = {
Expand All @@ -317,14 +317,10 @@ export class GitService implements Disposable {
}
catch (ex) {
if (RepoSearchWarnings.doesNotExist.test(ex.message || emptyStr)) {
Logger.log(
`Repository search (depth=${depth}) in '${uri.fsPath}' FAILED${
ex.message ? `(${ex.message})` : emptyStr
}`
);
Logger.log(cc, `FAILED${ex.message ? ` Error: ${ex.message}` : emptyStr}`);
}
else {
Logger.error(ex, `Repository search (depth=${depth}) in '${uri.fsPath}' FAILED`);
Logger.error(ex, cc, 'FAILED');
}

return repositories;
Expand All @@ -335,19 +331,15 @@ export class GitService implements Disposable {
// If we are the same as the root, skip it
if (Strings.normalizePath(p) === rootPath) continue;

Logger.log(cc, `searching in '${p}'...`);

const rp = await this.getRepoPathCore(p, true);
if (rp === undefined) continue;

Logger.log(`Repository found in '${rp}'`);
Logger.log(cc, `found repository in '${rp}'`);
repositories.push(new Repository(folder, rp, false, anyRepoChangedFn, this._suspended));
}

Logger.log(
`Completed repository search (depth=${depth}) in '${uri.fsPath}' ${
GlyphChars.Dot
} ${Strings.getDurationMilliseconds(start)} ms`
);

return repositories;
}

Expand Down Expand Up @@ -449,6 +441,8 @@ export class GitService implements Disposable {

@log()
async applyChangesToWorkingFile(uri: GitUri, ref1?: string, ref2?: string) {
const cc = Logger.getCorrelationContext();

ref1 = ref1 || uri.sha;
if (ref1 === undefined || uri.repoPath === undefined) return;

Expand Down Expand Up @@ -484,7 +478,7 @@ export class GitService implements Disposable {
}
}

Logger.error(ex);
Logger.error(ex, cc);
void Messages.showGenericErrorMessage('Unable to apply changes');
}
}
Expand Down Expand Up @@ -1663,14 +1657,16 @@ export class GitService implements Disposable {

@log()
async getMergeBase(repoPath: string, ref1: string, ref2: string, options: { forkPoint?: boolean } = {}) {
const cc = Logger.getCorrelationContext();

try {
const data = await Git.merge_base(repoPath, ref1, ref2, options);
if (data === undefined) return undefined;

return data.split('\n')[0];
}
catch (ex) {
Logger.error(ex);
Logger.error(ex, cc);
return undefined;
}
}
Expand Down Expand Up @@ -1708,7 +1704,7 @@ export class GitService implements Disposable {

async getRepoPath(filePath: string, options?: { ref?: string }): Promise<string | undefined>;
async getRepoPath(uri: Uri | undefined, options?: { ref?: string }): Promise<string | undefined>;
@log({
@log<GitService['getRepoPath']>({
exit: path => `returned ${path}`
})
async getRepoPath(
Expand Down Expand Up @@ -1828,7 +1824,7 @@ export class GitService implements Disposable {
repoPathOrUri: string | Uri,
options?: { ref?: string; skipCacheUpdate?: boolean }
): Promise<Repository | undefined>;
@log({
@log<GitService['getRepository']>({
exit: repo => `returned ${repo !== undefined ? `${repo.path}` : 'undefined'}`
})
async getRepository(
Expand Down Expand Up @@ -2019,8 +2015,8 @@ export class GitService implements Disposable {
options?: { ref?: string; skipCacheUpdate?: boolean }
): Promise<boolean>;
async isTracked(uri: GitUri): Promise<boolean>;
@log({
exit: tracked => `returned ${tracked.toString()}`,
@log<GitService['isTracked']>({
exit: tracked => `returned ${tracked}`,
singleLine: true
})
async isTracked(
Expand Down
24 changes: 13 additions & 11 deletions src/system/decorators/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ export function logName<T>(fn: (c: T, name: string) => string) {
};
}

export function debug<T>(
export function debug<T extends (...arg: any) => any>(
options: {
args?: false | { [arg: string]: (arg: any) => string | false };
condition?(...args: any[]): boolean;
condition?(...args: Parameters<T>): boolean;
correlate?: boolean;
enter?(...args: any[]): string;
exit?(result: any): string;
prefix?(context: LogContext<T>, ...args: any[]): string;
enter?(...args: Parameters<T>): string;
exit?(result: PromiseType<ReturnType<T>>): string;
prefix?(context: LogContext<T>, ...args: Parameters<T>): string;
sanitize?(key: string, value: any): any;
singleLine?: boolean;
timed?: boolean;
Expand All @@ -63,15 +63,17 @@ export function debug<T>(
return log<T>({ debug: true, ...options });
}

export function log<T>(
type PromiseType<T> = T extends Promise<infer U> ? U : T;

export function log<T extends (...arg: any) => any>(
options: {
args?: false | { [arg: number]: (arg: any) => string | false };
condition?(...args: any[]): boolean;
condition?(...args: Parameters<T>): boolean;
correlate?: boolean;
debug?: boolean;
enter?(...args: any[]): string;
exit?(result: any): string;
prefix?(context: LogContext<T>, ...args: any[]): string;
enter?(...args: Parameters<T>): string;
exit?(result: PromiseType<ReturnType<T>>): string;
prefix?(context: LogContext<T>, ...args: Parameters<T>): string;
sanitize?(key: string, value: any): any;
singleLine?: boolean;
timed?: boolean;
Expand All @@ -95,7 +97,7 @@ export function log<T>(

const parameters = Functions.getParameters(fn);

descriptor.value = function(this: any, ...args: any[]) {
descriptor.value = function(this: any, ...args: Parameters<T>) {
const correlationId = getNextCorrelationId();

if (
Expand Down

0 comments on commit fb36df4

Please sign in to comment.