From b9843b64336e8e1ac90f811b9dda95fb9771c9d7 Mon Sep 17 00:00:00 2001 From: Charlie Greenman Date: Mon, 7 Oct 2024 06:21:30 -0400 Subject: [PATCH] ZETA-9187: Complete documentation feature --- src/documentation/documentation.ts | 33 +++++++++++++++++++++++------- src/extension.ts | 1 - 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/documentation/documentation.ts b/src/documentation/documentation.ts index 66a7def..7301f95 100644 --- a/src/documentation/documentation.ts +++ b/src/documentation/documentation.ts @@ -1,6 +1,7 @@ import * as vscode from 'vscode'; import { exec } from 'child_process'; import { promisify } from 'util'; +import * as path from 'path'; const execAsync = promisify(exec); @@ -16,26 +17,44 @@ export async function getGitHistoryAndNavigateToRazrooUrl() { const lineNumber = editor.selection.active.line + 1; try { + // Find the git root directory + const { stdout: gitRoot } = await execAsync('git rev-parse --show-toplevel', { cwd: path.dirname(filePath) }); + const gitRootPath = gitRoot.trim(); + + // Get relative path of the file from git root + const relativeFilePath = path.relative(gitRootPath, filePath); + // Get Git blame information - const { stdout: blameOutput } = await execAsync(`git blame -L ${lineNumber},${lineNumber} "${filePath}"`); + const { stdout: blameOutput } = await execAsync(`git blame -L ${lineNumber},${lineNumber} "${relativeFilePath}"`, { cwd: gitRootPath }); const commitHash = blameOutput.split(' ')[0]; - // Get commit details - const { stdout: commitDetails } = await execAsync(`git show --format="%B" -s ${commitHash}`); + const { stdout: commitDetails } = await execAsync(`git show --format="%B" -s ${commitHash}`, { cwd: gitRootPath }); + console.log('commitDetails', commitDetails); // Extract URL from commit message - const urlMatch = commitDetails.match(/https?:\/\/razroo\.com\S+/); + const urlMatch = commitDetails.match(/razroo\.com\S+/); + console.log('urlMatch', urlMatch); if (!urlMatch) { vscode.window.showInformationMessage('No Razroo URL found in the commit message.'); return; } - const url = urlMatch[0]; + const url = urlMatch[0].replace(/\]$/, ''); + + console.log('url', url); // Open URL in default browser - vscode.env.openExternal(vscode.Uri.parse(url)); + vscode.env.openExternal(vscode.Uri.parse(`https://${url}`)).then(() => { + vscode.window.showInformationMessage(`Opened ${url} in your default browser.`); + }, (error) => { + vscode.window.showErrorMessage(`Failed to open ${url}: ${error}`); + }); } catch (error: any) { - vscode.window.showErrorMessage(`Error: ${error.message}`); + if (error.message.includes('not a git repository')) { + vscode.window.showErrorMessage('The current file is not in a Git repository.'); + } else { + vscode.window.showErrorMessage(`Error: ${error.message}`); + } } } \ No newline at end of file diff --git a/src/extension.ts b/src/extension.ts index 8856961..07f0a0d 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -169,7 +169,6 @@ export async function activate(context: vscode.ExtensionContext) { async() => { try { getGitHistoryAndNavigateToRazrooUrl(); - console.log('show documentation called'); } catch (error) { console.log('COMMAND_TRY_TO_AUTH ERROR'); console.error(error);