Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/extension/handlers/scan-repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { simpleGit } from "simple-git";
import * as vscode from "vscode";

import { extConfig } from "@/extension/config";
import { logger } from "@/extension/util/logger";
import type { GitRepo, ScanRepoResult } from "@/types";

export async function scanRepos(): Promise<ScanRepoResult> {
const workspaceDirs = (vscode.workspace.workspaceFolders ?? []).map((f) => f.uri.fsPath);
const repos = await startScan(extConfig.gitBinary(), workspaceDirs, extConfig.maxDepth());
logger.info(`Repository scan completed: ${repos.length} found`);

return {
repos
Expand Down
6 changes: 3 additions & 3 deletions src/extension/rpc/rpc-notify.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from "vscode";

import { logger } from "@/old-extension/utils/logger";
import { logger } from "@/extension/util/logger";
import type { RpcNotification, RpcNotificationMap, RpcNotificationName } from "@/types";

let _webview: vscode.Webview | undefined;
Expand All @@ -11,7 +11,7 @@ export const rpcNotify = {
message: RpcNotificationMap[N]
): Promise<void> {
if (_webview === undefined) {
logger.log(`Skip RPC notification: ${name}; webview is not initialized`);
logger.debug(`Skip RPC notification: ${name}; webview is not initialized`);
return;
}

Expand All @@ -22,7 +22,7 @@ export const rpcNotify = {
message
} as RpcNotification<N>;

logger.log(`Send RPC notification: ${name}`);
logger.debug(`Send RPC notification: ${name}`);
await _webview.postMessage(payload);
}
};
Expand Down
6 changes: 6 additions & 0 deletions src/extension/rpc/rpc-server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from "vscode";

import { logger } from "@/extension/util/logger";
import type { RpcMethod, RpcResponse } from "@/types";

import { rpcHandlers } from "./handlers";
Expand Down Expand Up @@ -36,8 +37,10 @@ export function createRpcServer() {
if (!isRpcRequest(message)) {
return;
}
logger.debug(`RPC request received: ${message.method} (${message.id})`);

if (!isRpcMethod(message.method)) {
logger.warn(`Unknown RPC method: ${message.method}`);
const response: RpcResponse = {
kind: "rpc.response",
id: message.id,
Expand All @@ -46,6 +49,7 @@ export function createRpcServer() {
};

await webview.postMessage(response);
logger.debug(`RPC response sent: ${message.method} (${message.id}, failure)`);
return;
}

Expand All @@ -60,7 +64,9 @@ export function createRpcServer() {
};

await webview.postMessage(response);
logger.debug(`RPC response sent: ${message.method} (${message.id}, success)`);
} catch (err) {
logger.error(`RPC method failed: ${message.method}`, err);
const response: RpcResponse = {
kind: "rpc.response",
id: message.id,
Expand Down
4 changes: 2 additions & 2 deletions src/extension/util/debounce.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as vscode from "vscode";

import { logger } from "@/old-extension/utils/logger";
import { logger } from "@/extension/util/logger";

export type FsWatcherEvent = "created" | "deleted";

Expand All @@ -25,7 +25,7 @@ export function createDebouncer() {
setTimeout(() => {
timers.delete(key);
void callback(type, uri).catch((error: unknown) => {
logger.log(`Unable to process repository change: ${String(error)}`);
logger.error("Unable to process repository change", error);
});
}, 100)
);
Expand Down
28 changes: 28 additions & 0 deletions src/extension/util/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as vscode from "vscode";

import { EXTENSION_NAME } from "@/extension/constants";

let _channel: vscode.LogOutputChannel | undefined;

export const logger = {
init: (ctx: vscode.ExtensionContext) => {
_channel = vscode.window.createOutputChannel(EXTENSION_NAME, { log: true });
ctx.subscriptions.push(_channel);
},

error: (message: string | Error, ...args: unknown[]) => {
_channel?.error(message, ...args);
},

warn: (message: string, ...args: unknown[]) => {
_channel?.warn(message, ...args);
},

info: (message: string, ...args: unknown[]) => {
_channel?.info(message, ...args);
},

debug: (message: string, ...args: unknown[]) => {
_channel?.debug(message, ...args);
}
};
1 change: 0 additions & 1 deletion src/extension/view-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export function createViewCommand(ctx: vscode.ExtensionContext) {
]
}
);

webPanel.iconPath =
extConfig.tabIconColourTheme() === "colour"
? vscode.Uri.joinPath(ctx.extensionUri, "resources", "webview-icon.svg")
Expand Down
14 changes: 7 additions & 7 deletions src/extension/watchers/git-repo.watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from "node:path";
import * as vscode from "vscode";

import { rpcNotify } from "@/extension/rpc/rpc-notify";
import { logger } from "@/old-extension/utils/logger";
import { logger } from "@/extension/util/logger";

const REFRESH_DELAY = 750;
const GIT_DATA = /^(HEAD|config|index|packed-refs|refs(?:\/.*)?)$/;
Expand Down Expand Up @@ -35,7 +35,7 @@ export function watchGitRepo(): vscode.Disposable {
repoPath = repo;
watcher = vscode.workspace.createFileSystemWatcher(new vscode.RelativePattern(repo, "**/*"));

const refresh = (uri: vscode.Uri) => {
const refresh = (event: "created" | "changed" | "deleted", uri: vscode.Uri) => {
if (muteDepth > 0 || Date.now() < resumeAt) {
return;
}
Expand All @@ -48,20 +48,20 @@ export function watchGitRepo(): vscode.Disposable {
return;
}

logger.log(`Git repository file changed: ${uri.fsPath}`);
logger.debug(`Repository file ${event}: ${uri.fsPath}`);
if (refreshTimer !== undefined) {
clearTimeout(refreshTimer);
}
refreshTimer = setTimeout(() => {
refreshTimer = undefined;
logger.log(`Git repository changed: ${repo}`);
logger.debug(`Sending repo.updated notification: ${repo}`);
void rpcNotify.notify("repo.updated", { path: repo });
}, REFRESH_DELAY);
};

watcher.onDidCreate(refresh);
watcher.onDidChange(refresh);
watcher.onDidDelete(refresh);
watcher.onDidCreate((uri) => refresh("created", uri));
watcher.onDidChange((uri) => refresh("changed", uri));
watcher.onDidDelete((uri) => refresh("deleted", uri));
};

return new vscode.Disposable(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/extension/watchers/git.watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as vscode from "vscode";

import { rpcNotify } from "@/extension/rpc/rpc-notify";
import { createDebouncer, type FsWatcherEvent } from "@/extension/util/debounce";
import { logger } from "@/old-extension/utils/logger";
import { logger } from "@/extension/util/logger";

export function watchGitDir(): vscode.Disposable {
const debouncer = createDebouncer();
Expand All @@ -19,7 +19,7 @@ export function watchGitDir(): vscode.Disposable {
}

async function processGitDir(type: FsWatcherEvent, uri: vscode.Uri) {
logger.log(`Git directory ${type}: ${uri.fsPath}`);
logger.info(`Git directory ${type}: ${uri.fsPath}`);
const repoPath = path.dirname(uri.fsPath);

if (type === "created") {
Expand Down
6 changes: 5 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import * as vscode from "vscode";

import { EXTENSION_NAME } from "./extension/constants";
import { logger } from "./extension/util/logger";
import { createViewCommand } from "./extension/view-command";
import { logger } from "./old-extension/utils/logger";
import { legacyLogger } from "./old-extension/utils/logger";

export function activate(ctx: vscode.ExtensionContext) {
if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length <= 0) {
return;
}
logger.init(ctx);
legacyLogger.init(ctx);

const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
statusBarItem.name = EXTENSION_NAME;
Expand All @@ -22,4 +24,6 @@ export function activate(ctx: vscode.ExtensionContext) {
ctx.subscriptions.push(
vscode.commands.registerCommand("neo-git-graph.view", createViewCommand(ctx))
);

logger.info("Extension activated");
}
8 changes: 5 additions & 3 deletions src/old-extension/initExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { registerMessageHandlers } from "@/old-extension/messageHandler";
import { createRepoManager } from "@/old-extension/repoManager";
import type { RepoManager } from "@/old-extension/repoManager";
import { StatusBarItem } from "@/old-extension/statusBarItem";
import { logger } from "@/old-extension/utils/logger";
import { legacyLogger } from "@/old-extension/utils/logger";
import { webviewBridgeFactory } from "@/old-extension/webviewBridge";
import type { WebviewBridge } from "@/old-extension/webviewBridge";
import { createWebviewPanel } from "@/old-extension/webviewPanel";
Expand Down Expand Up @@ -90,7 +90,7 @@ export function initExtension(
statusBarItem: StatusBarItem
) {
try {
logger.log(`Initializing extension with ${repos.length} repo(s)`);
legacyLogger.log(`Initializing extension with ${repos.length} repo(s)`);

const extensionState = new ExtensionState(ctx);
const avatarManager = new AvatarManager(config.gitPath, extensionState);
Expand Down Expand Up @@ -178,7 +178,9 @@ export function initExtension(
})
);
} catch (err) {
logger.log(`Error during initialization: ${err instanceof Error ? err.message : String(err)}`);
legacyLogger.log(
`Error during initialization: ${err instanceof Error ? err.message : String(err)}`
);
throw err;
}
}
20 changes: 10 additions & 10 deletions src/old-extension/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,37 @@ import { getGitVersion } from "@/backend/utils/git";
import { config } from "@/old-extension/config";
import { initExtension } from "@/old-extension/initExtension";
import { StatusBarItem } from "@/old-extension/statusBarItem";
import { logger } from "@/old-extension/utils/logger";
import { legacyLogger } from "@/old-extension/utils/logger";
import { watchForRepos } from "@/old-extension/watchForRepos";

export async function activate(ctx: vscode.ExtensionContext) {
logger.init(ctx);
logger.log("Starting Neo Git Graph ...");
legacyLogger.init(ctx);
legacyLogger.log("Starting Neo Git Graph ...");

const gitPath = config.gitPath();
const gitVersion = await getGitVersion(gitPath);
if (gitVersion) {
logger.log(`Using git (version: ${gitVersion})`);
legacyLogger.log(`Using git (version: ${gitVersion})`);
} else {
logger.log("Failed to detect git version");
legacyLogger.log("Failed to detect git version");
}

const statusBarItem = new StatusBarItem(ctx, config);
statusBarItem.refresh();

const paths = (vscode.workspace.workspaceFolders ?? []).map((f) => f.uri.fsPath);
logger.log(`Searching workspace for new repos (${paths.length} folder(s)) ...`);
legacyLogger.log(`Searching workspace for new repos (${paths.length} folder(s)) ...`);
const repoDirs = await findGitRepos(paths, gitPath, config.maxDepthOfRepoSearch());

if (repoDirs.length > 0) {
logger.log(`Found ${repoDirs.length} repo(s)`);
legacyLogger.log(`Found ${repoDirs.length} repo(s)`);
initExtension(ctx, repoDirs, statusBarItem);
logger.log("Started Neo Git Graph - Ready to use!");
legacyLogger.log("Started Neo Git Graph - Ready to use!");
return;
}

logger.log("No repos found");
logger.log("Watching for new repos ...");
legacyLogger.log("No repos found");
legacyLogger.log("Watching for new repos ...");
ctx.subscriptions.push(watchForRepos(ctx, initExtension, statusBarItem));
}

Expand Down
14 changes: 9 additions & 5 deletions src/old-extension/statusBarItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as vscode from "vscode";

import type { Config } from "./config";
import { EXTENSION_NAME } from "./constant/const";
import { logger } from "./utils/logger";
import { legacyLogger } from "./utils/logger";

export class StatusBarItem {
private statusBarItem: vscode.StatusBarItem;
Expand All @@ -15,21 +15,23 @@ export class StatusBarItem {
this.statusBarItem.name = EXTENSION_NAME;
this.statusBarItem.command = "neo-git-graph.view";
context.subscriptions.push(this.statusBarItem);
logger.log(
legacyLogger.log(
`StatusBarItem created (showStatusBarItem=${config.showStatusBarItem()}, numRepos=0)`
);
}

public setNumRepos(numRepos: number) {
logger.log(`StatusBarItem.setNumRepos(${numRepos})`);
legacyLogger.log(`StatusBarItem.setNumRepos(${numRepos})`);
this.numRepos = numRepos;
this.refresh();
}

public refresh() {
const show = this.config.showStatusBarItem();
if (show) {
logger.log(`StatusBarItem.show() (showStatusBarItem=${show}, numRepos=${this.numRepos})`);
legacyLogger.log(
`StatusBarItem.show() (showStatusBarItem=${show}, numRepos=${this.numRepos})`
);
if (this.numRepos === 0) {
this.statusBarItem.text = `$(eye) ${EXTENSION_NAME}`;
this.statusBarItem.tooltip = vscode.l10n.t("No Git repository found — watching for one");
Expand All @@ -39,7 +41,9 @@ export class StatusBarItem {
}
this.statusBarItem.show();
} else {
logger.log(`StatusBarItem.hide() (showStatusBarItem=${show}, numRepos=${this.numRepos})`);
legacyLogger.log(
`StatusBarItem.hide() (showStatusBarItem=${show}, numRepos=${this.numRepos})`
);
this.statusBarItem.hide();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/old-extension/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as vscode from "vscode";

let _channel: vscode.OutputChannel | undefined;

export const logger = {
export const legacyLogger = {
init: (ctx: vscode.ExtensionContext) => {
_channel = vscode.window.createOutputChannel("Neo Git Graph");
ctx.subscriptions.push(_channel);
Expand Down