Skip to content

Commit fb36df4

Browse files
committed
Improves logging of repo search at start
1 parent 1d924e2 commit fb36df4

File tree

2 files changed

+42
-44
lines changed

2 files changed

+42
-44
lines changed

src/git/gitService.ts

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
// eslint-disable-next-line import/no-unresolved
2222
import { API as BuiltInGitApi, GitExtension } from '../@types/git';
2323
import { configuration, RemotesConfig } from '../configuration';
24-
import { CommandContext, DocumentSchemes, GlyphChars, setCommandContext } from '../constants';
24+
import { CommandContext, DocumentSchemes, setCommandContext } from '../constants';
2525
import { Container } from '../container';
2626
import { LogCorrelationContext, Logger } from '../logger';
2727
import { Messages } from '../messages';
@@ -265,32 +265,32 @@ export class GitService implements Disposable {
265265
}
266266
}
267267

268+
@log<GitService['repositorySearch']>({
269+
args: false,
270+
singleLine: true,
271+
prefix: (context, folder) => `${context.prefix}(${folder.uri.fsPath})`,
272+
exit: result =>
273+
`returned ${result.length} repositories${
274+
result.length !== 0 ? ` (${result.map(r => r.path).join(', ')})` : emptyStr
275+
}`
276+
})
268277
private async repositorySearch(folder: WorkspaceFolder): Promise<Repository[]> {
278+
const cc = Logger.getCorrelationContext();
269279
const { uri } = folder;
270280
const depth = configuration.get<number>(configuration.name('advanced')('repositorySearchDepth').value, uri);
271281

272-
Logger.log(`Searching for repositories (depth=${depth}) in '${uri.fsPath}' ...`);
273-
274-
const start = process.hrtime();
282+
Logger.log(cc, `searching (depth=${depth})...`);
275283

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

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

285-
if (depth <= 0) {
286-
Logger.log(
287-
`Completed repository search (depth=${depth}) in '${uri.fsPath}' ${
288-
GlyphChars.Dot
289-
} ${Strings.getDurationMilliseconds(start)} ms`
290-
);
291-
292-
return repositories;
293-
}
293+
if (depth <= 0) return repositories;
294294

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

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

334+
Logger.log(cc, `searching in '${p}'...`);
335+
338336
const rp = await this.getRepoPathCore(p, true);
339337
if (rp === undefined) continue;
340338

341-
Logger.log(`Repository found in '${rp}'`);
339+
Logger.log(cc, `found repository in '${rp}'`);
342340
repositories.push(new Repository(folder, rp, false, anyRepoChangedFn, this._suspended));
343341
}
344342

345-
Logger.log(
346-
`Completed repository search (depth=${depth}) in '${uri.fsPath}' ${
347-
GlyphChars.Dot
348-
} ${Strings.getDurationMilliseconds(start)} ms`
349-
);
350-
351343
return repositories;
352344
}
353345

@@ -449,6 +441,8 @@ export class GitService implements Disposable {
449441

450442
@log()
451443
async applyChangesToWorkingFile(uri: GitUri, ref1?: string, ref2?: string) {
444+
const cc = Logger.getCorrelationContext();
445+
452446
ref1 = ref1 || uri.sha;
453447
if (ref1 === undefined || uri.repoPath === undefined) return;
454448

@@ -484,7 +478,7 @@ export class GitService implements Disposable {
484478
}
485479
}
486480

487-
Logger.error(ex);
481+
Logger.error(ex, cc);
488482
void Messages.showGenericErrorMessage('Unable to apply changes');
489483
}
490484
}
@@ -1663,14 +1657,16 @@ export class GitService implements Disposable {
16631657

16641658
@log()
16651659
async getMergeBase(repoPath: string, ref1: string, ref2: string, options: { forkPoint?: boolean } = {}) {
1660+
const cc = Logger.getCorrelationContext();
1661+
16661662
try {
16671663
const data = await Git.merge_base(repoPath, ref1, ref2, options);
16681664
if (data === undefined) return undefined;
16691665

16701666
return data.split('\n')[0];
16711667
}
16721668
catch (ex) {
1673-
Logger.error(ex);
1669+
Logger.error(ex, cc);
16741670
return undefined;
16751671
}
16761672
}
@@ -1708,7 +1704,7 @@ export class GitService implements Disposable {
17081704

17091705
async getRepoPath(filePath: string, options?: { ref?: string }): Promise<string | undefined>;
17101706
async getRepoPath(uri: Uri | undefined, options?: { ref?: string }): Promise<string | undefined>;
1711-
@log({
1707+
@log<GitService['getRepoPath']>({
17121708
exit: path => `returned ${path}`
17131709
})
17141710
async getRepoPath(
@@ -1828,7 +1824,7 @@ export class GitService implements Disposable {
18281824
repoPathOrUri: string | Uri,
18291825
options?: { ref?: string; skipCacheUpdate?: boolean }
18301826
): Promise<Repository | undefined>;
1831-
@log({
1827+
@log<GitService['getRepository']>({
18321828
exit: repo => `returned ${repo !== undefined ? `${repo.path}` : 'undefined'}`
18331829
})
18341830
async getRepository(
@@ -2019,8 +2015,8 @@ export class GitService implements Disposable {
20192015
options?: { ref?: string; skipCacheUpdate?: boolean }
20202016
): Promise<boolean>;
20212017
async isTracked(uri: GitUri): Promise<boolean>;
2022-
@log({
2023-
exit: tracked => `returned ${tracked.toString()}`,
2018+
@log<GitService['isTracked']>({
2019+
exit: tracked => `returned ${tracked}`,
20242020
singleLine: true
20252021
})
20262022
async isTracked(

src/system/decorators/log.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ export function logName<T>(fn: (c: T, name: string) => string) {
4747
};
4848
}
4949

50-
export function debug<T>(
50+
export function debug<T extends (...arg: any) => any>(
5151
options: {
5252
args?: false | { [arg: string]: (arg: any) => string | false };
53-
condition?(...args: any[]): boolean;
53+
condition?(...args: Parameters<T>): boolean;
5454
correlate?: boolean;
55-
enter?(...args: any[]): string;
56-
exit?(result: any): string;
57-
prefix?(context: LogContext<T>, ...args: any[]): string;
55+
enter?(...args: Parameters<T>): string;
56+
exit?(result: PromiseType<ReturnType<T>>): string;
57+
prefix?(context: LogContext<T>, ...args: Parameters<T>): string;
5858
sanitize?(key: string, value: any): any;
5959
singleLine?: boolean;
6060
timed?: boolean;
@@ -63,15 +63,17 @@ export function debug<T>(
6363
return log<T>({ debug: true, ...options });
6464
}
6565

66-
export function log<T>(
66+
type PromiseType<T> = T extends Promise<infer U> ? U : T;
67+
68+
export function log<T extends (...arg: any) => any>(
6769
options: {
6870
args?: false | { [arg: number]: (arg: any) => string | false };
69-
condition?(...args: any[]): boolean;
71+
condition?(...args: Parameters<T>): boolean;
7072
correlate?: boolean;
7173
debug?: boolean;
72-
enter?(...args: any[]): string;
73-
exit?(result: any): string;
74-
prefix?(context: LogContext<T>, ...args: any[]): string;
74+
enter?(...args: Parameters<T>): string;
75+
exit?(result: PromiseType<ReturnType<T>>): string;
76+
prefix?(context: LogContext<T>, ...args: Parameters<T>): string;
7577
sanitize?(key: string, value: any): any;
7678
singleLine?: boolean;
7779
timed?: boolean;
@@ -95,7 +97,7 @@ export function log<T>(
9597

9698
const parameters = Functions.getParameters(fn);
9799

98-
descriptor.value = function(this: any, ...args: any[]) {
100+
descriptor.value = function(this: any, ...args: Parameters<T>) {
99101
const correlationId = getNextCorrelationId();
100102

101103
if (

0 commit comments

Comments
 (0)